Example #1
0
    def test_lr_eq_0_cov_gt_0(self):
        # If a vulnerability function loss ratio is 0 and its corresponding CoV
        # is > 0, a ValueError should be raised
        vuln_content = gettemp(u"""\
<?xml version='1.0' encoding='utf-8'?>
<nrml xmlns="http://openquake.org/xmlns/nrml/0.4"
      xmlns:gml="http://www.opengis.net/gml">
    <vulnerabilityModel>
        <discreteVulnerabilitySet vulnerabilitySetID="PAGER"
                                  assetCategory="population"
                                  lossCategory="occupants">
            <IML IMT="PGV">0.005 0.007 0.0098 0.0137</IML>
            <discreteVulnerability vulnerabilityFunctionID="A"
                                   probabilisticDistribution="LN">
                <lossRatio>0.00 0.06 0.18 0.36</lossRatio>
                <coefficientsVariation>0.30 0.30 0.30 0.30
                </coefficientsVariation>
            </discreteVulnerability>
        </discreteVulnerabilitySet>
    </vulnerabilityModel>
</nrml>
""")
        with self.assertRaises(ValueError) as ar:
            nrml.to_python(vuln_content)
        self.assertIn(
            'It is not valid to define a mean loss ratio = 0 '
            'with a corresponding coefficient of variation > 0',
            str(ar.exception))
Example #2
0
 def test_dupl_values_hddist(self):
     testfile = os.path.join(testdir, 'wrong-hddist.xml')
     with self.assertRaises(ValueError) as ctx:
         config.general.strict = True
         nrml.to_python(testfile)
     self.assertIn('There are repeated values in [5.5, 16.5, 16.5]',
                   str(ctx.exception))
Example #3
0
 def test_wrong_trt(self):
     """ Test consistency between group and sources TRTs """
     testfile = os.path.join(testdir, 'wrong-trt.xml')
     with self.assertRaises(ValueError) as ctx:
         nrml.to_python(testfile)
     self.assertIn('node pointSource: Found Cratonic, expected '
                   'Active Shallow Crust, line 67', str(ctx.exception))
Example #4
0
 def test_wrong_trt(self):
     """ Test consistency between group and sources TRTs """
     testfile = os.path.join(testdir, 'wrong-trt.xml')
     with self.assertRaises(ValueError) as ctx:
         nrml.to_python(testfile)
     self.assertIn('node pointSource: Found Cratonic, expected '
                   'Active Shallow Crust, line 67', str(ctx.exception))
Example #5
0
    def test_lr_eq_0_cov_gt_0(self):
        # If a vulnerability function loss ratio is 0 and its corresponding CoV
        # is > 0, a ValueError should be raised
        vuln_content = gettemp(u"""\
<?xml version='1.0' encoding='utf-8'?>
<nrml xmlns="http://openquake.org/xmlns/nrml/0.4"
      xmlns:gml="http://www.opengis.net/gml">
    <vulnerabilityModel>
        <discreteVulnerabilitySet vulnerabilitySetID="PAGER"
                                  assetCategory="population"
                                  lossCategory="occupants">
            <IML IMT="PGV">0.005 0.007 0.0098 0.0137</IML>
            <discreteVulnerability vulnerabilityFunctionID="A"
                                   probabilisticDistribution="LN">
                <lossRatio>0.00 0.06 0.18 0.36</lossRatio>
                <coefficientsVariation>0.30 0.30 0.30 0.30
                </coefficientsVariation>
            </discreteVulnerability>
        </discreteVulnerabilitySet>
    </vulnerabilityModel>
</nrml>
""")
        with self.assertRaises(ValueError) as ar:
            nrml.to_python(vuln_content)
        self.assertIn('It is not valid to define a loss ratio = 0.0 with a '
                      'corresponding coeff. of variation > 0.0',
                      str(ar.exception))
 def test_dupl_values_hddist(self):
     testfile = os.path.join(testdir, 'wrong-hddist.xml')
     with self.assertRaises(ValueError) as ctx:
         config.general.strict = True
         nrml.to_python(testfile)
     self.assertIn('There are repeated values in [5.5, 16.5, 16.5]',
                   str(ctx.exception))
Example #7
0
    def test_duplicated_id_vm(self):
        fname = gettemp('''\
<?xml version="1.0" encoding="UTF-8"?>
<nrml xmlns="http://openquake.org/xmlns/nrml/0.5">
<vulnerabilityModel id="vm-hazus" assetCategory="buildings" lossCategory="structural">
<description> structural vulnerability model for global database</description>

<vulnerabilityFunction id="CR-PC_LWAL-DNO_H1" dist="LN">
<imls imt="PGA"> 0.05 0.051933 0.053941 0.056026 0.058192 0.060442 0.062778 0.065205 0.067726 0.070344 0.073064 0.075889 0.078822 0.08187 0.085035 0.088322 0.091737 0.095283 0.098967 0.102793 0.106767 0.110894 0.115181 0.119634 0.124259 0.129063 0.134053 0.139235 0.144618 0.150209 0.156016 0.162047 0.168312 0.174819 0.181577 0.188597 0.195888 0.203461 0.211327 0.219497 0.227982 0.236796 0.245951 0.255459 0.265335 0.275593 0.286247 0.297313 0.308807 0.320746 0.333146 0.346025 0.359402 0.373296 0.387728 0.402717 0.418286 0.434457 0.451253 0.468699 0.486818 0.505639 0.525186 0.54549 0.566578 0.588482 0.611233 0.634863 0.659406 0.684899 0.711377 0.738878 0.767443 0.797112 0.827928 0.859936 0.893181 0.927711 0.963576 1.00083 1.03952 1.07971 1.12145 1.1648 1.20983 1.2566 1.30519 1.35564 1.40805 1.46249 1.51903 1.57775 1.63875 1.7021 1.7679 1.83625 1.90724 1.98097 2.05756 2.1371 </imls>
<meanLRs> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1e-06 1e-06 1e-06 2e-06 4e-06 6e-06 1e-05 1.5e-05 2.3e-05 3.5e-05 5.2e-05 7.6e-05 0.000112 0.000161 0.000229 0.000321 0.000445 0.00061 0.000826 0.001105 0.00146 0.001907 0.002463 0.003145 0.003973 0.004966 0.006143 0.007525 0.009131 0.010981 0.013094 0.015492 0.018197 0.021233 0.02463 0.028422 0.032651 0.037367 0.042629 0.048508 0.055089 0.062465 0.070742 0.080036 0.090473 0.102183 0.115298 0.129951 0.146268 0.164362 0.184333 0.206256 0.230179 0.256115 0.284042 0.31389 0.345547 0.378852 0.413597 0.449529 0.486356 0.52375 0.56136 0.598817 0.63575 0.671796 0.70661 0.739878 0.771326 0.800729 0.827913 0.852763 0.875219 0.895278 0.912986 0.928433 0.941747 </meanLRs>
<covLRs> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 </covLRs>
</vulnerabilityFunction>

<vulnerabilityFunction dist="LN" id="CR-PC_LWAL-DUM_H1">
<imls imt="PGA">0.05 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1 1.05 1.1 1.15 1.2 1.25 1.3 1.35 1.4 1.45 1.5 1.55 1.6 1.65 1.7 1.75 1.8 1.85 1.9 1.95 2 2.05 2.1 2.15 2.2 2.25 2.3 2.35 2.4 2.45 2.5 2.55 2.6 2.7 2.75 2.85 2.95 3.05 3.15 3.3 3.5 3.7 4 5</imls>
<meanLRs>0.000 0.005 0.010 0.018 0.030 0.045 0.062 0.083 0.106 0.132 0.159 0.188 0.217 0.247 0.278 0.308 0.338 0.368 0.397 0.426 0.454 0.480 0.506 0.531 0.555 0.5786 0.600 0.621 0.641 0.660 0.678 0.696 0.712 0.727 0.742 0.756 0.769 0.782 0.793 0.804 0.815 0.825 0.834 0.843 0.851 0.859 0.867 0.874 0.887 0.893 0.904 0.913 0.922 0.930 0.940 0.951 0.960 0.970 0.988</meanLRs>
<covLRs>0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000</covLRs>
</vulnerabilityFunction>
	
<vulnerabilityFunction dist="LN" id="CR-PC_LWAL-DNO_H1">
<imls imt="PGA">0.05 0.200 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1 1.05 1.1 1.15 1.2 1.25 1.3 1.35 1.4 1.45 1.5 1.55 1.6 1.65 1.7 1.75 1.8 1.85 1.9 1.95 2 2.05 2.1 2.15 2.2 2.25 2.3 2.35 2.4 2.45 2.5 2.55 2.6 2.85 3.1 3.55 4.9</imls>
<meanLRs>0.000 0.050 0.122 0.165 0.210 0.256 0.302 0.347 0.390 0.431 0.469 0.505 0.539 0.5717 0.600 0.628 0.653 0.676 0.698 0.718 0.737 0.754 0.770 0.785 0.798 0.811 0.823 0.834 0.844 0.854 0.862 0.871 0.878 0.889 0.8927 0.8986 0.904 0.909 0.914 0.919 0.923 0.928 0.931 0.9355 0.938 0.942 0.945 0.948 0.9509 0.961 0.970 0.980 0.993</meanLRs>
<covLRs>0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000</covLRs>
</vulnerabilityFunction>

</vulnerabilityModel>
</nrml>''')
        with self.assertRaises(nrml.InvalidFile) as ctx:
            nrml.to_python(fname)
        self.assertIn('Duplicated vulnerabilityFunctionID: CR-PC_LWAL-DNO_H1',
                      str(ctx.exception))
    def test_load_mfs(self):
        datadir = os.path.join(os.path.dirname(__file__), 'data', 'sections')
        sec_xml = os.path.join(datadir, 'sections_kite.xml')
        src_xml = os.path.join(datadir, 'sources.xml')
        conv = SourceConverter()
        sec = nrml.to_python(sec_xml, conv).sections
        expected = [Point(11, 45, 0), Point(11, 45.5, 10)]
        # Check geometry info
        self.assertEqual(expected[0], sec[1].surface.profiles[0].points[0])
        self.assertEqual(expected[1], sec[1].surface.profiles[0].points[1])
        self.assertEqual(sec[1].sec_id, 's2')
        ssm = nrml.to_python(src_xml, conv)
        self.assertIsInstance(ssm, nrml.SourceModel)
        ssm[0][0].create_inverted_index(sec)  # fix sections
        rups = list(ssm[0][0].iter_ruptures())

        # Check data for the second rupture
        msg = 'Rake for rupture #0 is wrong'
        self.assertEqual(rups[0].rake, 90.0, msg)
        # Check data for the second rupture
        msg = 'Rake for rupture #1 is wrong'
        self.assertEqual(rups[1].rake, -90.0, msg)
        # Check mfd
        expected = numpy.array([0.9, 0.1])
        numpy.testing.assert_almost_equal(rups[1].probs_occur, expected)
Example #9
0
 def test_wrong_trt(self):
     # a group with sources of two different TRTs
     testfile = os.path.join(testdir, 'wrong-trt.xml')
     with self.assertRaises(ValueError) as ctx:
         nrml.to_python(testfile)
     self.assertIn(
         'node pointSource: Found Cratonic, expected '
         'Active Shallow Crust, line 67', str(ctx.exception))
Example #10
0
    def test_hypolist_but_not_sliplist(self):
        simple_file = BytesIO(b"""\
<?xml version='1.0' encoding='utf-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
    <sourceModel name="Some Source Model">
        <simpleFaultSource
        id="3"
        name="Mount Diablo Thrust"
        tectonicRegion="Active Shallow Crust"
        >
            <simpleFaultGeometry>
                <gml:LineString>
                    <gml:posList>
                        -121.8229 37.7301 -122.0388 37.8771
                    </gml:posList>
                </gml:LineString>
                <dip>
                    45.0
                </dip>
                <upperSeismoDepth>
                    10.0
                </upperSeismoDepth>
                <lowerSeismoDepth>
                    20.0
                </lowerSeismoDepth>
            </simpleFaultGeometry>
            <magScaleRel>
                WC1994
            </magScaleRel>
            <ruptAspectRatio>
                1.5
            </ruptAspectRatio>
            <incrementalMFD
            binWidth="0.1"
            minMag="5.0"
            >
                <occurRates>
                    0.0010614989 0.00088291627 0.00073437777 0.0006108288 0.0005080653
                </occurRates>
            </incrementalMFD>
            <rake>
                30.0
            </rake>
            <hypoList>
                <hypo alongStrike="0.25" downDip="0.25" weight="0.3"/>
                <hypo alongStrike="0.75" downDip="0.75" weight="0.7"/>
            </hypoList>
        </simpleFaultSource>
    </sourceModel>
</nrml>
""")
        # check that the error raised by hazardlib is wrapped correctly
        msg = ('node simpleFaultSource: hypo_list and slip_list have to be '
               'both given')
        with self.assertRaises(ValueError) as ctx:
            nrml.to_python(simple_file, self.conv)
        self.assertIn(msg, str(ctx.exception))
Example #11
0
    def test_hypolist_but_not_sliplist(self):
        simple_file = BytesIO(b"""\
<?xml version='1.0' encoding='utf-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
    <sourceModel name="Some Source Model">
        <simpleFaultSource
        id="3"
        name="Mount Diablo Thrust"
        tectonicRegion="Active Shallow Crust"
        >
            <simpleFaultGeometry>
                <gml:LineString>
                    <gml:posList>
                        -121.8229 37.7301 -122.0388 37.8771
                    </gml:posList>
                </gml:LineString>
                <dip>
                    45.0
                </dip>
                <upperSeismoDepth>
                    10.0
                </upperSeismoDepth>
                <lowerSeismoDepth>
                    20.0
                </lowerSeismoDepth>
            </simpleFaultGeometry>
            <magScaleRel>
                WC1994
            </magScaleRel>
            <ruptAspectRatio>
                1.5
            </ruptAspectRatio>
            <incrementalMFD
            binWidth="0.1"
            minMag="5.0"
            >
                <occurRates>
                    0.0010614989 0.00088291627 0.00073437777 0.0006108288 0.0005080653
                </occurRates>
            </incrementalMFD>
            <rake>
                30.0
            </rake>
            <hypoList>
                <hypo alongStrike="0.25" downDip="0.25" weight="0.3"/>
                <hypo alongStrike="0.75" downDip="0.75" weight="0.7"/>
            </hypoList>
        </simpleFaultSource>
    </sourceModel>
</nrml>
""")
        # check that the error raised by hazardlib is wrapped correctly
        msg = ('node simpleFaultSource: hypo_list and slip_list have to be '
               'both given')
        with self.assertRaises(ValueError) as ctx:
            nrml.to_python(simple_file, self.conv)
        self.assertIn(msg, str(ctx.exception))
Example #12
0
 def test_duplicate_id(self):
     conv = s.SourceConverter(
         investigation_time=50.,
         rupture_mesh_spacing=1,
         complex_fault_mesh_spacing=1,
         width_of_mfd_bin=0.1,
         area_source_discretization=10,
     )
     with self.assertRaises(nrml.DuplicatedID):
         nrml.to_python(DUPLICATE_ID_SRC_MODEL, conv)
Example #13
0
 def test(self):
     fname = os.path.join(os.path.dirname(__file__),
                          'faults_backg_source_model.xml')
     # check that the input file requires the fix indeed
     with self.assertRaises(ValueError):
         nrml.to_python(fname)
     fd, tmpname = tempfile.mkstemp(suffix='.xml')
     os.close(fd)
     fix(fname, tmpname)  # invoke the fix
     print('meld %s %s' % (fname, tmpname))
Example #14
0
 def test_duplicate_id(self):
     conv = s.SourceConverter(
         investigation_time=50.,
         rupture_mesh_spacing=1,
         complex_fault_mesh_spacing=1,
         width_of_mfd_bin=0.1,
         area_source_discretization=10,
     )
     with self.assertRaises(nrml.DuplicatedID):
         nrml.to_python(DUPLICATE_ID_SRC_MODEL, conv)
Example #15
0
def validate_nrml(request):
    """
    Leverage oq-risklib to check if a given XML text is a valid NRML

    :param request:
        a `django.http.HttpRequest` object containing the mandatory
        parameter 'xml_text': the text of the XML to be validated as NRML

    :returns: a JSON object, containing:
        * 'valid': a boolean indicating if the provided text is a valid NRML
        * 'error_msg': the error message, if any error was found
                       (None otherwise)
        * 'error_line': line of the given XML where the error was found
                        (None if no error was found or if it was not a
                        validation error)
    """
    xml_text = request.POST.get('xml_text')
    if not xml_text:
        return HttpResponseBadRequest(
            'Please provide the "xml_text" parameter')
    xml_file = gettemp(xml_text, suffix='.xml')
    try:
        nrml.to_python(xml_file)
    except ExpatError as exc:
        return _make_response(error_msg=str(exc),
                              error_line=exc.lineno,
                              valid=False)
    except Exception as exc:
        # get the exception message
        exc_msg = exc.args[0]
        if isinstance(exc_msg, bytes):
            exc_msg = exc_msg.decode('utf-8')  # make it a unicode object
        elif isinstance(exc_msg, str):
            pass
        else:
            # if it is another kind of object, it is not obvious a priori how
            # to extract the error line from it
            return _make_response(error_msg=str(exc_msg),
                                  error_line=None,
                                  valid=False)
        # if the line is not mentioned, the whole message is taken
        error_msg = exc_msg.split(', line')[0]
        # check if the exc_msg contains a line number indication
        search_match = re.search(r'line \d+', exc_msg)
        if search_match:
            error_line = int(search_match.group(0).split()[1])
        else:
            error_line = None
        return _make_response(error_msg=error_msg,
                              error_line=error_line,
                              valid=False)
    else:
        return _make_response(error_msg=None, error_line=None, valid=True)
Example #16
0
def validate_nrml(request):
    """
    Leverage oq-risklib to check if a given XML text is a valid NRML

    :param request:
        a `django.http.HttpRequest` object containing the mandatory
        parameter 'xml_text': the text of the XML to be validated as NRML

    :returns: a JSON object, containing:
        * 'valid': a boolean indicating if the provided text is a valid NRML
        * 'error_msg': the error message, if any error was found
                       (None otherwise)
        * 'error_line': line of the given XML where the error was found
                        (None if no error was found or if it was not a
                        validation error)
    """
    xml_text = request.POST.get('xml_text')
    if not xml_text:
        return HttpResponseBadRequest(
            'Please provide the "xml_text" parameter')
    xml_file = gettemp(xml_text, suffix='.xml')
    try:
        nrml.to_python(xml_file)
    except ExpatError as exc:
        return _make_response(error_msg=str(exc),
                              error_line=exc.lineno,
                              valid=False)
    except Exception as exc:
        # get the exception message
        exc_msg = exc.args[0]
        if isinstance(exc_msg, bytes):
            exc_msg = exc_msg.decode('utf-8')   # make it a unicode object
        elif isinstance(exc_msg, str):
            pass
        else:
            # if it is another kind of object, it is not obvious a priori how
            # to extract the error line from it
            return _make_response(
                error_msg=str(exc_msg), error_line=None, valid=False)
        # if the line is not mentioned, the whole message is taken
        error_msg = exc_msg.split(', line')[0]
        # check if the exc_msg contains a line number indication
        search_match = re.search(r'line \d+', exc_msg)
        if search_match:
            error_line = int(search_match.group(0).split()[1])
        else:
            error_line = None
        return _make_response(
            error_msg=error_msg, error_line=error_line, valid=False)
    else:
        return _make_response(error_msg=None, error_line=None, valid=True)
Example #17
0
    def test_invalid_vf_pmf(self):
        fname = writetmp('''\
<?xml version="1.0" encoding="UTF-8"?> 
<nrml xmlns="http://openquake.org/xmlns/nrml/0.5"> 
	<vulnerabilityModel id="Vulnerabilidad" assetCategory="MI_AIS" lossCategory="structural"> 
		<description>Vulnerabilidad Microcomponentes</description> 
		<vulnerabilityFunction id="MI_PTR" dist="PM">
			<imls imt="PGA">0.00 0.10 0.20 0.31 0.41 0.51 0.61 0.71 0.82 0.92 1.02 1.12 1.22 1.33 1.43 1.53 </imls>
			<probabilities lr="0">1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.000000000017">0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.000024">0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.005">0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.067">0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.25">0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.50">0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.72">0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.86">0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.93">0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.97">0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0</probabilities>
			<probabilities lr="0.99">0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0</probabilities>
			<probabilities lr="0.995">0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0</probabilities>
			<probabilities lr="0.998">0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0</probabilities>
			<probabilities lr="0.999">0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0</probabilities>
			<probabilities lr="1">0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1</probabilities>
		</vulnerabilityFunction>
		<vulnerabilityFunction id="MI_TON" dist="PM">
			<imls imt="PGA">0.00 0.10 0.20 0.31 0.41 0.51 0.61 0.71 0.82 0.92 1.02 1.12 1.22 1.33 </imls>
			<probabilities lr="0">1 0 0 0 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.0000317">0 1 0 0 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.025">0 0 1 0 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.0221">0 0 0 1 0 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.531">0 0 0 0 1 0 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.768">0 0 0 0 0 1 0 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.898">0 0 0 0 0 0 1 0 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.958">0 0 0 0 0 0 0 1 0 0 0 0 0 0</probabilities>
			<probabilities lr="0.983">0 0 0 0 0 0 0 0 1 0 0 0 0 0</probabilities>
			<probabilities lr="0.993">0 0 0 0 0 0 0 0 0 1 0 0 0 0</probabilities>
			<probabilities lr="0.997">0 0 0 0 0 0 0 0 0 0 1 0 0 0</probabilities>
			<probabilities lr="0.998">0 0 0 0 0 0 0 0 0 0 0 1 0 0</probabilities>
			<probabilities lr="0.999">0 0 0 0 0 0 0 0 0 0 0 0 1 0</probabilities>
			<probabilities lr="1">0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1</probabilities>
		</vulnerabilityFunction>
	</vulnerabilityModel> 
</nrml>''')
        with self.assertRaises(ValueError) as ctx:
            nrml.to_python(fname)
        self.assertIn('Wrong number of probabilities (expected 14, got 17)',
                      str(ctx.exception))
    def test_nankai(self):
        # source model for the Nankai region provided by M. Pagani
        source_model = os.path.join(os.path.dirname(__file__), 'nankai.xml')
        # it has a single group containing 15 mutex sources
        [group] = nrml.to_python(source_model)
        aae([src.mutex_weight for src in group],
            [0.0125, 0.0125, 0.0125, 0.0125, 0.1625, 0.1625, 0.0125, 0.0125,
             0.025, 0.025, 0.05, 0.05, 0.325, 0.025, 0.1])
        seed = 42
        start = 0
        for i, src in enumerate(group):
            src.id = i
            nr = src.num_ruptures
            src.serial = start + seed
            start += nr
        param = dict(ses_per_logic_tree_path=10, filter_distance='rjb',
                     gsims=[SiMidorikawa1999SInter()])
        sf = calc.filters.SourceFilter(None, {})
        dic = sum(sample_ruptures(group, sf, param), {})
        self.assertEqual(len(dic['rup_array']), 5)
        self.assertEqual(len(dic['calc_times']), 15)  # mutex sources

        # test no filtering 1
        ruptures = list(stochastic_event_set(group))
        self.assertEqual(len(ruptures), 19)

        # test no filtering 2
        ruptures = sum(sample_ruptures(group, sf, param), {})['rup_array']
        self.assertEqual(len(ruptures), 5)
Example #19
0
def get_ltmodels(oq, gsim_lt, source_model_lt, h5=None):
    """
    Build source models from the logic tree and to store
    them inside the `source_info` dataset.
    """
    if oq.pointsource_distance['default'] == {}:
        spinning_off = False
    else:
        spinning_off = sum(oq.pointsource_distance.values()) == 0
    if spinning_off:
        logging.info('Removing nodal plane and hypocenter distributions')
    # NB: the source models file are often NOT in the shared directory
    # (for instance in oq-engine/demos) so the processpool must be used
    dist = ('no' if os.environ.get('OQ_DISTRIBUTE') == 'no'
            else 'processpool')
    smlt_dir = os.path.dirname(source_model_lt.filename)
    converter = sourceconverter.SourceConverter(
        oq.investigation_time, oq.rupture_mesh_spacing,
        oq.complex_fault_mesh_spacing, oq.width_of_mfd_bin,
        oq.area_source_discretization, oq.minimum_magnitude,
        not spinning_off, oq.source_id)
    rlzs = source_model_lt.get_eff_rlzs()
    lt_models = []
    for rlz in rlzs:
        ltm = LtSourceModel(
            rlz['value'], rlz['weight'], rlz['lt_path'], [],
            rlz['ordinal'], rlz['samples'], rlz['offset'])
        lt_models.append(ltm)
    if oq.calculation_mode.startswith('ucerf'):
        idx = 0
        [grp] = nrml.to_python(oq.inputs["source_model"], converter)
        for grp_id, ltm in enumerate(lt_models):
            sg = copy.copy(grp)
            sg.id = grp_id
            ltm.src_groups = [sg]
            src = sg[0].new(ltm.ordinal, ltm.names)  # one source
            src.src_group_id = grp_id
            idx += 1
            src.samples = ltm.samples
            sg.sources = [src]
            data = [((grp_id, grp_id, src.source_id, src.code,
                      0, 0, -1, src.num_ruptures, 0, ''))]
            sg.info = numpy.array(data, source_info_dt)
        return lt_models

    logging.info('Reading the source model(s) in parallel')
    allargs = []
    fileno = 0
    for rlz in rlzs:
        for name in rlz['value'].split():
            fname = os.path.abspath(os.path.join(smlt_dir, name))
            allargs.append((rlz['ordinal'], rlz['lt_path'],
                            source_model_lt.apply_uncertainties, fname,
                            fileno))
            fileno += 1
    smap = parallel.Starmap(
        SourceReader(converter, smlt_dir, h5),
        allargs, distribute=dist, h5=h5 if h5 else None)
    # NB: h5 is None in logictree_test.py
    return _store_results(smap, lt_models, source_model_lt, gsim_lt, oq, h5)
Example #20
0
 def test_tom_poisson_no_rate(self):
     testfile = os.path.join(testdir, 'tom_poisson_no_rate.xml')
     sc = SourceConverter(area_source_discretization=10.)
     sg = nrml.to_python(testfile, sc)
     src = sg[0].sources[0]
     msg = "Wrong time span in the temporal occurrence model"
     self.assertEqual(src.temporal_occurrence_model.time_span, 50, msg)
Example #21
0
    def test_nankai(self):
        # source model for the Nankai region provided by M. Pagani
        source_model = os.path.join(os.path.dirname(__file__), 'nankai.xml')
        # it has a single group containing 15 mutex sources
        [group] = nrml.to_python(source_model)
        for i, src in enumerate(group):
            src.id = i
            src.grp_id = 0
        aae([src.mutex_weight for src in group], [
            0.0125, 0.0125, 0.0125, 0.0125, 0.1625, 0.1625, 0.0125, 0.0125,
            0.025, 0.025, 0.05, 0.05, 0.325, 0.025, 0.1
        ])
        param = dict(ses_per_logic_tree_path=10, ses_seed=42, imtls={})
        cmaker = contexts.ContextMaker('*', [SiMidorikawa1999SInter()], param)
        dic = sum(sample_ruptures(group, cmaker), {})
        self.assertEqual(len(dic['rup_array']), 8)
        self.assertEqual(len(dic['calc_times']), 15)  # mutex sources

        # test no filtering 1
        ruptures = list(stochastic_event_set(group))
        self.assertEqual(len(ruptures), 19)

        # test no filtering 2
        ruptures = sum(sample_ruptures(group, cmaker), {})['rup_array']
        self.assertEqual(len(ruptures), 8)
Example #22
0
 def test_magnitude_bins(self):
     """ Testing build disaggregation matrix """
     fname = os.path.join(DATA_PATH, 'data', 'ssm.xml')
     converter = sourceconverter.SourceConverter(50., 1., 10, 0.1, 10)
     groups = to_python(fname, converter)
     sources = []
     for g in groups:
         sources += g.sources
     site = Site(Point(172.63, -43.53), vs30=250, vs30measured=False,
                 z1pt0=330)
     imt = SA(3.0)
     iml = 0.25612220
     gsim_by_trt = {TRT.ACTIVE_SHALLOW_CRUST: Bradley2013()}
     truncation_level = 3.0
     n_epsilons = 1
     mag_bin_width = 0.1
     dist_bin_width = 100.
     coord_bin_width = 100.
     # Compute the disaggregation matrix
     edges, mtx = disagg.disaggregation(sources, site, imt, iml,
                                        gsim_by_trt, truncation_level,
                                        n_epsilons, mag_bin_width,
                                        dist_bin_width, coord_bin_width)
     tm = disagg.mag_pmf(mtx[:, :, :, :, :, 0])
     numpy.testing.assert_array_less(numpy.zeros_like(tm[2:]), tm[2:])
Example #23
0
 def test_is_writeable(self):
     groups = [copy.deepcopy(grp)
               for grp in nrml.to_python(ALT_MFDS, conv)]
     # there are a SimpleFaultSource and a CharacteristicFaultSource
     fd, fname = tempfile.mkstemp(suffix='.xml')
     with os.fdopen(fd, 'wb'):
         write_source_model(fname, groups, 'Test Source Model')
Example #24
0
    def test_nankai(self):
        # source model for the Nankai region provided by M. Pagani
        source_model = os.path.join(os.path.dirname(__file__), 'nankai.xml')
        # it has a single group containing 15 mutex sources
        [group] = nrml.to_python(source_model)
        aae(group.srcs_weights, [
            0.0125, 0.0125, 0.0125, 0.0125, 0.1625, 0.1625, 0.0125, 0.0125,
            0.025, 0.025, 0.05, 0.05, 0.325, 0.025, 0.1
        ])
        rup_serial = numpy.arange(group.tot_ruptures, dtype=numpy.uint32)
        start = 0
        for i, src in enumerate(group):
            src.id = i
            nr = src.num_ruptures
            src.serial = rup_serial[start:start + nr]
            start += nr
        group.samples = 1
        lonlat = 135.68, 35.68
        site = Site(geo.Point(*lonlat), 800, True, z1pt0=100., z2pt5=1.)
        s_filter = SourceFilter(SiteCollection([site]), {})
        param = dict(ses_per_logic_tree_path=10, seed=42)
        gsims = [SiMidorikawa1999SInter()]
        dic = sample_ruptures(group, s_filter, gsims, param)
        self.assertEqual(dic['num_ruptures'], 19)  # total ruptures
        self.assertEqual(dic['num_events'], 16)
        self.assertEqual(len(dic['eb_ruptures']), 8)
        self.assertEqual(len(dic['calc_times']), 15)  # mutex sources

        # test export
        mesh = numpy.array([lonlat], [('lon', float), ('lat', float)])
        ebr = dic['eb_ruptures'][0]
        ebr.export(mesh)
Example #25
0
    def test_nankai(self):
        # source model for the Nankai region provided by M. Pagani
        source_model = os.path.join(os.path.dirname(__file__), 'nankai.xml')
        # it has a single group containing 15 mutex sources
        [group] = nrml.to_python(source_model)
        aae([src.mutex_weight for src in group],
            [0.0125, 0.0125, 0.0125, 0.0125, 0.1625, 0.1625, 0.0125, 0.0125,
             0.025, 0.025, 0.05, 0.05, 0.325, 0.025, 0.1])
        seed = 42
        start = 0
        for i, src in enumerate(group):
            src.id = i
            nr = src.num_ruptures
            src.serial = start + seed
            start += nr
        param = dict(ses_per_logic_tree_path=10, filter_distance='rjb',
                     gsims=[SiMidorikawa1999SInter()])
        sf = calc.filters.SourceFilter(None, {})
        dic = sum(sample_ruptures(group, sf, param), {})
        self.assertEqual(len(dic['rup_array']), 6)
        self.assertEqual(len(dic['calc_times']), 15)  # mutex sources

        # test no filtering 1
        ruptures = list(stochastic_event_set(group))
        self.assertEqual(len(ruptures), 19)

        # test no filtering 2
        ruptures = sum(sample_ruptures(group, sf, param), {})['rup_array']
        self.assertEqual(len(ruptures), 6)
Example #26
0
 def test_magnitude_bins(self):
     """ Testing build disaggregation matrix """
     fname = os.path.join(DATA_PATH, 'data', 'ssm.xml')
     converter = sourceconverter.SourceConverter(50., 1., 10, 0.1, 10)
     groups = to_python(fname, converter)
     sources = []
     for g in groups:
         sources += g.sources
     site = Site(Point(172.63, -43.53),
                 vs30=250,
                 vs30measured=False,
                 z1pt0=330)
     imt = SA(3.0)
     iml = 0.25612220
     gsim_by_trt = {TRT.ACTIVE_SHALLOW_CRUST: Bradley2013()}
     truncation_level = 3.0
     n_epsilons = 1
     mag_bin_width = 0.1
     dist_bin_width = 100.
     coord_bin_width = 100.
     # Compute the disaggregation matrix
     edges, mtx = disagg.disaggregation(sources, site, imt, iml,
                                        gsim_by_trt, truncation_level,
                                        n_epsilons, mag_bin_width,
                                        dist_bin_width, coord_bin_width)
     tm = disagg.mag_pmf(mtx[:, :, :, :, :, 0])
     numpy.testing.assert_array_less(numpy.zeros_like(tm[2:]), tm[2:])
Example #27
0
    def test_case_17(self):  # oversampling and save_ruptures
        # also, grp-00 does not produce ruptures
        expected = [
            'hazard_curve-mean.csv',
            'hazard_curve-rlz-001.csv',
            'hazard_curve-rlz-002.csv',
            'hazard_curve-rlz-003.csv',
            'hazard_curve-rlz-004.csv',
        ]
        # test the --hc functionality, i.e. that ruptures are read correctly
        out = self.run_calc(case_17.__file__, 'job.ini,job.ini', exports='csv')
        fnames = out['hcurves', 'csv']
        for exp, got in zip(expected, fnames):
            self.assertEqualFiles('expected/%s' % exp, got)

        # check that a single rupture file is exported even if there are
        # several collections
        [fname] = export(('ruptures', 'xml'), self.calc.datastore.parent)
        self.assertEqualFiles('expected/ses.xml', fname)

        # check that the exported file is parseable
        rupcoll = nrml.to_python(fname, RuptureConverter(1))
        self.assertEqual(list(rupcoll), [1])  # one group
        self.assertEqual(len(rupcoll[1]), 3)  # three EBRuptures

        # check that GMFs are not stored
        with self.assertRaises(KeyError):
            self.calc.datastore['gmf_data']
Example #28
0
    def from_model_files(cls,
                         limits,
                         input_model,
                         investigation_time=1.0,
                         simple_mesh_spacing=1.0,
                         complex_mesh_spacing=5.0,
                         mfd_width=0.1,
                         area_discretisation=10.0):
        """
        Reads the hazard model from a file

        :param list limits:
             Grid configuration [west, east, xspc, south, north, yspc,
                                 upper, lower, zspc]
        :param str input_model:
            Path to input source model
        :param float investigation_time:
            Investigation time of Poisson model
        :param float simple_mesh_spacing:
            Rupture mesh spacing of simple fault (km)
        :param float complex_mesh_spacing:
            Rupture mesh spacing of complex fault (km)
        :param float mfd_width:
            Spacing (in magnitude units) of MFD
        :param float area_discretisation:
            Spacing of discretisation of area source (km)
        """
        converter = SourceConverter(investigation_time, simple_mesh_spacing,
                                    complex_mesh_spacing, mfd_width,
                                    area_discretisation)
        sources = []
        for grp in nrml.to_python(input_model, converter):
            sources.extend(grp.sources)
        return cls(limits, sources, area_discretisation)
Example #29
0
    def test_nankai(self):
        # source model for the Nankai region provided by M. Pagani
        source_model = os.path.join(os.path.dirname(__file__), 'nankai.xml')
        # it has a single group containing 15 mutex sources
        [group] = nrml.to_python(source_model)
        aae([src.mutex_weight for src in group],
            [0.0125, 0.0125, 0.0125, 0.0125, 0.1625, 0.1625, 0.0125, 0.0125,
             0.025, 0.025, 0.05, 0.05, 0.325, 0.025, 0.1])
        seed = 42
        rup_serial = numpy.arange(seed, seed + group.tot_ruptures,
                                  dtype=numpy.uint32)
        start = 0
        for i, src in enumerate(group):
            src.id = i
            nr = src.num_ruptures
            src.serial = rup_serial[start:start + nr]
            start += nr
        lonlat = 135.68, 35.68
        site = Site(geo.Point(*lonlat), 800, z1pt0=100., z2pt5=1.)
        s_filter = SourceFilter(SiteCollection([site]), {})
        param = dict(ses_per_logic_tree_path=10, filter_distance='rjb',
                     gsims=[SiMidorikawa1999SInter()])
        dic = sum(sample_ruptures(group, param, s_filter), {})
        self.assertEqual(len(dic['rup_array']), 5)
        self.assertEqual(len(dic['calc_times']), 15)  # mutex sources

        # test no filtering 1
        ruptures = list(stochastic_event_set(group))
        self.assertEqual(len(ruptures), 19)

        # test no filtering 2
        ruptures = sum(sample_ruptures(group, param), {})['rup_array']
        self.assertEqual(len(ruptures), 5)
Example #30
0
def _get_source_model(source_file, investigation_time=1.,
                      rupture_mesh_spacing=10.0,
                      complex_fault_mesh_spacing=10.0,
                      width_of_mfd_bin=0.1,
                      area_source_discretization=20.,
                      **kwargs):
    """
    Read and build a source model from an xml file

    :parameter source_file
        The name of a file containing the source model
    :parameter float inv_time:
        A positive float
    :paramater float simple_mesh_spacing:
        A positive float
    :parameter float complex_mesh_spacing:
        A positive float
    :parameter float mfd_spacing:
        A positive float
    :parameter float area_discretisation:
        A positive float
    :returns:
        A list of :class:`~openquake.hazardlib.sourceconverter.SourceGroup`
        instances
    """
    conv = SourceConverter(investigation_time, rupture_mesh_spacing, 
                           complex_fault_mesh_spacing,
                           width_of_mfd_bin, area_source_discretization,
                           **kwargs)
    srcs = to_python(source_file, conv)
    return srcs.src_groups
Example #31
0
 def test_tom_poisson_no_rate(self):
     testfile = os.path.join(testdir, 'tom_poisson_no_rate.xml')
     sc = SourceConverter(area_source_discretization=10.)
     sg = nrml.to_python(testfile, sc)
     src = sg[0].sources[0]
     msg = "Wrong time span in the temporal occurrence model"
     self.assertEqual(src.temporal_occurrence_model.time_span, 50, msg)
Example #32
0
 def test_tom_poisson_not_defined(self):
     """ Read area source without tom """
     testfile = os.path.join(testdir, 'area-source.xml')
     sc = SourceConverter(area_source_discretization=10.)
     sg = nrml.to_python(testfile, sc)
     src = sg[0].sources[0]
     self.assertEqual(src.temporal_occurrence_model.time_span, 50,
                      "Wrong time span in the temporal occurrence model")
Example #33
0
 def test_tom_poisson_not_defined(self):
     """ Read area source without tom """
     testfile = os.path.join(testdir, 'area-source.xml')
     sc = SourceConverter(area_source_discretization=10.)
     sg = nrml.to_python(testfile, sc)
     src = sg[0].sources[0]
     self.assertEqual(src.temporal_occurrence_model.time_span, 50,
                      "Wrong time span in the temporal occurrence model")
Example #34
0
 def test_source_group_with_tom(self):
     testfile = os.path.join(testdir, 'source_group_with_tom.xml')
     sc = SourceConverter(area_source_discretization=10.)
     sg = nrml.to_python(testfile, sc)
     msg = "Wrong occurrence rate in the temporal occurrence model"
     expected = sg[0].temporal_occurrence_model.occurrence_rate
     self.assertEqual(expected, 0.01, msg)
     msg = "Wrong cluster definition"
     self.assertEqual(sg[0].cluster, False, msg)
Example #35
0
 def test_non_parametric_mutex(self):
     """ Test non-parametric source with mutex ruptures """
     fname = 'nonparametric-source-mutex-ruptures.xml'
     testfile = os.path.join(testdir, fname)
     grp = nrml.to_python(testfile)[0]
     src = grp[0]
     expected = numpy.array([0.2, 0.8])
     computed = numpy.array([src.data[0][0].weight, src.data[1][0].weight])
     numpy.testing.assert_equal(computed, expected)
Example #36
0
    def test_ok(self):
        fname = os.path.join(EXAMPLES_DIR, 'consequence-model.xml')
        cmodel = nrml.to_python(fname)
        self.assertEqual(
            repr(cmodel),
            "<ConsequenceModel structural ds1, ds2, ds3, ds4 tax1>")

        # test pickleability
        pickle.loads(pickle.dumps(cmodel))
Example #37
0
 def test_source_group_with_tom(self):
     testfile = os.path.join(testdir, 'source_group_with_tom.xml')
     sc = SourceConverter(area_source_discretization=10.)
     sg = nrml.to_python(testfile, sc)
     msg = "Wrong occurrence rate in the temporal occurrence model"
     expected = sg[0].temporal_occurrence_model.occurrence_rate
     self.assertEqual(expected, 0.01, msg)
     msg = "Wrong cluster definition"
     self.assertEqual(sg[0].cluster, False, msg)
Example #38
0
def check_input(job_ini_or_zip_or_nrml):
    if job_ini_or_zip_or_nrml.endswith('.xml'):
        try:
            print(nrml.to_python(job_ini_or_zip_or_nrml))
        except Exception as exc:
            sys.exit(exc)
    else:
        calc = base.calculators(readinput.get_oqparam(job_ini_or_zip_or_nrml))
        calc.read_inputs()
Example #39
0
 def test_non_parametric_mutex(self):
     """ Test non-parametric source with mutex ruptures """
     fname = 'nonparametric-source-mutex-ruptures.xml'
     testfile = os.path.join(testdir, fname)
     grp = nrml.to_python(testfile)[0]
     src = grp[0]
     expected = numpy.array([0.2, 0.8])
     computed = numpy.array([src.data[0][0].weight, src.data[1][0].weight])
     numpy.testing.assert_equal(computed, expected)
 def test_serialization(self):
     testfile = os.path.join(testdir,
                             'nonparametric-source-mutex-ruptures.xml')
     [grp] = nrml.to_python(testfile)
     for i, src in enumerate(grp, 1):
         src.id = i
     with hdf5.File.temporary() as f:
         f['grp'] = grp
     with hdf5.File(f.path, 'r') as f:
         print(f['grp'])
Example #41
0
 def test_serialization(self):
     testfile = os.path.join(
         testdir, 'nonparametric-source-mutex-ruptures.xml')
     [grp] = nrml.to_python(testfile)
     for i, src in enumerate(grp, 1):
         src.id = i
     with hdf5.File.temporary() as f:
         f['grp'] = grp
     with hdf5.File(f.path, 'r') as f:
         print(f['grp'])
Example #42
0
 def test_toml(self):
     out = ''
     for fname in (MIXED, ALT_MFDS, MULTIPOINT):
         smodel = nrml.to_python(fname, conv)
         for sgroup in smodel:
             for src in sgroup:
                 out += tomldump(src)
     # NB: uncomment the line below to regenerate the TOML file
     # with open(TOML, 'w') as f: f.write(out)
     self.assertEqual(out, open(TOML).read())
Example #43
0
    def test_ok(self):
        EXAMPLES_DIR = os.path.dirname(nrml_examples.__file__)
        fname = os.path.join(EXAMPLES_DIR, 'consequence-model.xml')
        cmodel = nrml.to_python(fname)
        self.assertEqual(
            repr(cmodel),
            "<ConsequenceModel structural ds1, ds2, ds3, ds4 tax1>")

        # test pickleability
        pickle.loads(pickle.dumps(cmodel))
Example #44
0
 def setUpClass(cls):
     conv = s.SourceConverter(
         investigation_time=50.,
         rupture_mesh_spacing=1,  # km
         complex_fault_mesh_spacing=1,  # km
         width_of_mfd_bin=1.,  # for Truncated GR MFDs
         area_source_discretization=1.)
     cls.source_collector = {
         sc.trt: sc for sc in nrml.to_python(MIXED_SRC_MODEL, conv)}
     cls.sitecol = site.SiteCollection(cls.SITES)
Example #45
0
 def test_simple_fault_source(self):
     d = os.path.dirname(os.path.dirname(__file__))
     tmps = 'simple-fault-source.xml'
     source_model = os.path.join(d, 'source_model', tmps)
     groups = nrml.to_python(source_model, SourceConverter(
         investigation_time=50., rupture_mesh_spacing=2.))
     src = groups[0].sources[0]
     src.seed = 0
     rup = src.get_one_rupture()
     self.assertEqual(rup.mag, 5.2)
Example #46
0
 def setUpClass(cls):
     conv = s.SourceConverter(
         investigation_time=50.,
         rupture_mesh_spacing=1,  # km
         complex_fault_mesh_spacing=1,  # km
         width_of_mfd_bin=1.,  # for Truncated GR MFDs
         area_source_discretization=1.)
     cls.source_collector = {
         sc.trt: sc for sc in nrml.to_python(MIXED_SRC_MODEL, conv)}
     cls.sitecol = site.SiteCollection(cls.SITES)
 def setUp(self):
     testfile = os.path.join(DATA, 'source_group_cluster.xml')
     sc = SourceConverter(area_source_discretization=10.,
                          investigation_time=1.)
     # This provides a SourceModel
     self.sg = getattr(nrml.to_python(testfile, sc), 'src_groups')
     self.imtls = DictArray({'PGA': [0.01, 0.1, 0.2, 0.3, 1.0]})
     gsim = SadighEtAl1997()
     self.gsim_by_trt = {TRT.ACTIVE_SHALLOW_CRUST: gsim}
     site = Site(Point(1.0, -0.1), 800, z1pt0=30., z2pt5=1.)
     self.sites = SiteCollection([site])
Example #48
0
 def test_international_date_line_2(self):
     # from a bug affecting a calculation in New Zealand
     fname = gettemp(characteric_source)
     [[src]] = nrml.to_python(fname)
     os.remove(fname)
     maxdist = IntegrationDistance({'default': 200})
     sitecol = SiteCollection([
         Site(location=Point(176.919, -39.489),
              vs30=760, vs30measured=True, z1pt0=100, z2pt5=5)])
     srcfilter = SourceFilter(sitecol, maxdist)
     sites = srcfilter.get_close_sites(src)
     self.assertIsNotNone(sites)
 def test(self):
     source_model = os.path.join(os.path.dirname(__file__), 'nankai.xml')
     groups = nrml.to_python(source_model, SourceConverter(
         investigation_time=50., rupture_mesh_spacing=2.))
     site = Site(Point(135.68, 35.68), 400, z1pt0=100., z2pt5=1.)
     s_filter = SourceFilter(SiteCollection([site]), {})
     imtls = DictArray({'PGV': [20, 40, 80]})
     gsim_by_trt = {'Subduction Interface': SiMidorikawa1999SInter()}
     hcurves = calc_hazard_curves(groups, s_filter, imtls, gsim_by_trt)
     npt.assert_almost_equal(
         [1.1262869e-01, 3.9968668e-03, 3.1005840e-05],
         hcurves['PGV'][0])
Example #50
0
 def setUp(self):
     d = os.path.dirname(os.path.dirname(__file__))
     source_model = os.path.join(d, 'source_model/multi-point-source.xml')
     [self.sources] = nrml.to_python(source_model, SourceConverter(
         investigation_time=50., rupture_mesh_spacing=2.))
     self.site = Site(Point(0.1, 0.1), 800, z1pt0=100., z2pt5=1.)
     self.imt = PGA()
     self.iml = 0.1
     self.truncation_level = 1
     self.trt = 'Stable Continental Crust'
     gsim = Campbell2003()
     gsim.minimum_distance = 10  # test minimum_distance
     self.gsims = {self.trt: gsim}
Example #51
0
    def test_one_taxonomy_many_imts(self):
        # Should raise a ValueError if a vulnerabilityFunctionID is used for
        # multiple IMTs.
        # In this test input, we've defined two functions in separate sets
        # with the same ID and different IMTs.
        vuln_content = gettemp(u"""\
<?xml version='1.0' encoding='utf-8'?>
<nrml xmlns="http://openquake.org/xmlns/nrml/0.4"
      xmlns:gml="http://www.opengis.net/gml">
    <vulnerabilityModel>
        <discreteVulnerabilitySet vulnerabilitySetID="PAGER"
                                  assetCategory="population"
                                  lossCategory="occupants">
            <IML IMT="PGA">0.005 0.007 0.0098 0.0137</IML>
            <discreteVulnerability vulnerabilityFunctionID="A"
                                   probabilisticDistribution="LN">
                <lossRatio>0.01 0.06 0.18 0.36</lossRatio>
                <coefficientsVariation>0.30 0.30 0.30 0.30
         </coefficientsVariation>
            </discreteVulnerability>
        </discreteVulnerabilitySet>
        <discreteVulnerabilitySet vulnerabilitySetID="PAGER"
                                  assetCategory="population"
                                  lossCategory="occupants">
            <IML IMT="MMI">0.005 0.007 0.0098 0.0137</IML>
            <discreteVulnerability vulnerabilityFunctionID="A"
                                   probabilisticDistribution="LN">
                <lossRatio>0.01 0.06 0.18 0.36</lossRatio>
                <coefficientsVariation>0.30 0.30 0.30 0.30
                </coefficientsVariation>
            </discreteVulnerability>
        </discreteVulnerabilitySet>
    </vulnerabilityModel>
</nrml>
""")
        with self.assertRaises(InvalidFile) as ar:
            nrml.to_python(vuln_content)
        self.assertIn('Duplicated vulnerabilityFunctionID: A',
                      str(ar.exception))
Example #52
0
    def test_missing_maxIML(self):
        vuln_content = gettemp(u"""\
<?xml version='1.0' encoding='utf-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
     <fragilityModel format="continuous">
        <description>Fragility for test</description>
        <limitStates>LS1 LS2</limitStates>
        <ffs type="lognormal">
            <taxonomy>RC</taxonomy>
            <IML IMT="PGA" minIML="9.9" imlUnit="g"/>
            <ffc ls="LS1">
                <params mean="0.2" stddev="0.05" />
            </ffc>
            <ffc ls="LS2">
                <params mean="0.35" stddev="0.10" />
            </ffc>
        </ffs>
    </fragilityModel>
</nrml>""")
        with self.assertRaises(KeyError) as ar:
            nrml.to_python(vuln_content)
        self.assertIn("node IML: 'maxIML', line 9", str(ar.exception))
 def test(self):
     # mutually exclusive ruptures
     d = os.path.dirname(os.path.dirname(__file__))
     tmps = 'nonparametric-source-mutex-ruptures.xml'
     source_model = os.path.join(d, 'source_model', tmps)
     groups = nrml.to_python(source_model, SourceConverter(
         investigation_time=50., rupture_mesh_spacing=2.))
     site = Site(Point(143.5, 39.5), 800, z1pt0=100., z2pt5=1.)
     sitecol = SiteCollection([site])
     imtls = DictArray({'PGA': [0.01, 0.1, 0.2, 0.5]})
     gsim_by_trt = {'Some TRT': Campbell2003()}
     hcurves = calc_hazard_curves(groups, sitecol, imtls, gsim_by_trt)
     # expected results obtained with an ipython notebook
     expected = [4.3998728e-01, 1.1011728e-01, 7.5495312e-03, 8.5812844e-06]
     npt.assert_almost_equal(hcurves['PGA'][0], expected)
    def test_splittable_events(self):
        # split the events in two blocks and check that the ratios are
        # same: there is no randomness in VulnerabilityFunction.sample
        vuln_model = gettemp("""\
<?xml version='1.0' encoding='utf-8'?>
<nrml xmlns="http://openquake.org/xmlns/nrml/0.4"
      xmlns:gml="http://www.opengis.net/gml">
    <vulnerabilityModel>
        <discreteVulnerabilitySet vulnerabilitySetID="PAGER"
                                  assetCategory="Category"
                                  lossCategory="structural">
            <IML IMT="PGA">0.005 0.007 0.0098 0.0137</IML>
            <discreteVulnerability vulnerabilityFunctionID="RC/A"
                                   probabilisticDistribution="LN">
                <lossRatio>0.01 0.06 0.18 0.36</lossRatio>
                <coefficientsVariation>0.30 0.30 0.30 0.30
         </coefficientsVariation>
            </discreteVulnerability>
        </discreteVulnerabilitySet>
    </vulnerabilityModel>
</nrml>""")
        vfs = {('structural', 'vulnerability'):
               nrml.to_python(vuln_model)['PGA', 'RC/A']}
        rm = riskmodels.RiskModel('event_based_risk', "RC/A", vfs,
                                  ignore_covs=False)
        assets = [0, 1]
        eids = numpy.array([1, 2, 3, 4, 5])
        gmvs = numpy.array([.1, .2, .3, .4, .5])
        epsilons = numpy.array(
            [[.01, .02, .03, .04, .05], [.001, .002, .003, .004, .005]])

        # compute the ratios by considering all the events
        ratios = rm('structural', assets, gmvs, eids, epsilons)
        numpy.testing.assert_allclose(ratios, self.expected_ratios)

        # split the events in two blocks
        eids1 = numpy.array([1, 2])
        eids2 = numpy.array([3, 4, 5])
        gmvs1 = numpy.array([.1, .2])
        gmvs2 = numpy.array([.3, .4, .5])
        eps1 = numpy.array([[.01, .02], [.001, .002]])
        eps2 = numpy.array([[.03, .04, .05], [.003, .004, .005]])
        ratios1 = rm('structural', assets, gmvs1, eids1, eps1)
        ratios2 = rm('structural', assets, gmvs2, eids2, eps2)
        numpy.testing.assert_allclose(ratios1, self.expected_ratios[:, :2])
        numpy.testing.assert_allclose(ratios2, self.expected_ratios[:, 2:])
Example #55
0
    def setUpClass(cls):
        cls.conv = s.SourceConverter(
            investigation_time=50.,
            rupture_mesh_spacing=1,  # km
            complex_fault_mesh_spacing=1,  # km
            width_of_mfd_bin=1.,  # for Truncated GR MFDs
            area_source_discretization=1.,  # km
        )
        groups = nrml.to_python(MIXED_SRC_MODEL, cls.conv)
        ([cls.point], [cls.cmplx], [cls.area, cls.simple],
         [cls.char_simple, cls.char_complex, cls.char_multi]) = groups

        # the parameters here would typically be specified in the job .ini
        cls.investigation_time = 50.
        cls.rupture_mesh_spacing = 1  # km
        cls.complex_fault_mesh_spacing = 1  # km
        cls.width_of_mfd_bin = 1.  # for Truncated GR MFDs
        cls.area_source_discretization = 1.  # km
    def test(self):
        d = os.path.dirname(os.path.dirname(__file__))
        source_model = os.path.join(d, 'source_model/multi-point-source.xml')
        groups = nrml.to_python(source_model, SourceConverter(
            investigation_time=50., rupture_mesh_spacing=2.))
        site = Site(Point(0.1, 0.1), 800, z1pt0=100., z2pt5=1.)
        sitecol = SiteCollection([site])
        imtls = DictArray({'PGA': [0.01, 0.02, 0.04, 0.08, 0.16]})
        gsim_by_trt = {'Stable Continental Crust': Campbell2003()}
        hcurves = calc_hazard_curves(groups, sitecol, imtls, gsim_by_trt)
        expected = [0.99999778, 0.9084039, 0.148975348,
                    0.0036909656, 2.76326e-05]
        npt.assert_almost_equal(hcurves['PGA'][0], expected)

        # splitting in point sources
        [[mps1, mps2]] = groups
        psources = list(mps1) + list(mps2)
        hcurves = calc_hazard_curves(psources, sitecol, imtls, gsim_by_trt)
        npt.assert_almost_equal(hcurves['PGA'][0], expected)
Example #57
0
    def test_different_levels_ok(self):
        # the same IMT can appear with different levels in different
        # vulnerability functions
        vuln_content = gettemp(u"""\
<?xml version='1.0' encoding='utf-8'?>
<nrml xmlns="http://openquake.org/xmlns/nrml/0.4"
      xmlns:gml="http://www.opengis.net/gml">
    <vulnerabilityModel>
        <discreteVulnerabilitySet vulnerabilitySetID="PAGER"
                                  assetCategory="population"
                                  lossCategory="occupants">
            <IML IMT="PGA">0.005 0.007 0.0098 0.0137</IML>
            <discreteVulnerability vulnerabilityFunctionID="RC/A"
                                   probabilisticDistribution="LN">
                <lossRatio>0.01 0.06 0.18 0.36</lossRatio>
                <coefficientsVariation>0.30 0.30 0.30 0.30
         </coefficientsVariation>
            </discreteVulnerability>
        </discreteVulnerabilitySet>
        <discreteVulnerabilitySet vulnerabilitySetID="PAGER"
                                  assetCategory="population"
                                  lossCategory="occupants">
            <IML IMT="PGA">0.004 0.008 0.037</IML>
            <discreteVulnerability vulnerabilityFunctionID="RC/B"
                                   probabilisticDistribution="LN">
                <lossRatio>0.01 0.06 0.18</lossRatio>
                <coefficientsVariation>0.30 0.30 0.30
         </coefficientsVariation>
            </discreteVulnerability>
        </discreteVulnerabilitySet>
    </vulnerabilityModel>
</nrml>
""")
        vfs = nrml.to_python(vuln_content)
        assert_almost_equal(vfs['PGA', 'RC/A'].imls,
                            numpy.array([0.005, 0.007, 0.0098, 0.0137]))
        assert_almost_equal(vfs['PGA', 'RC/B'].imls,
                            numpy.array([0.004, 0.008, 0.037]))
Example #58
0
    def test_wrong_files(self):
        # missing lossCategory
        with self.assertRaises(KeyError) as ctx:
            nrml.to_python(self.wrong_csq_model_1)
        self.assertIn("node consequenceModel: 'lossCategory', line 3",
                      str(ctx.exception))

        # missing loss state
        with self.assertRaises(ValueError) as ctx:
            nrml.to_python(self.wrong_csq_model_2)
        self.assertIn("node consequenceFunction: Expected 4 limit"
                      " states, got 3, line 9", str(ctx.exception))

        # inverted loss states
        with self.assertRaises(ValueError) as ctx:
            nrml.to_python(self.wrong_csq_model_3)
        self.assertIn("node params: Expected 'ds3', got 'ds4', line 12",
                      str(ctx.exception))
    def test_case_17(self):  # oversampling and save_ruptures
        # also, the grp-00 does not produce ruptures
        expected = [
            'hazard_curve-mean.csv',
            'hazard_curve-rlz-001.csv',
            'hazard_curve-rlz-002.csv',
            'hazard_curve-rlz-003.csv',
            'hazard_curve-rlz-004.csv',
        ]
        # test the --hc functionality, i.e. that ruptures are read correctly
        out = self.run_calc(case_17.__file__, 'job.ini,job.ini', exports='csv')
        fnames = out['hcurves', 'csv']
        for exp, got in zip(expected, fnames):
            self.assertEqualFiles('expected/%s' % exp, got)

        # check that a single rupture file is exported even if there are
        # several collections
        [fname] = export(('ruptures', 'xml'), self.calc.datastore.parent)
        self.assertEqualFiles('expected/ses.xml', fname)

        # check that the exported file is parseable
        rupcoll = nrml.to_python(fname, RuptureConverter(1))
        self.assertEqual(list(rupcoll), [1])  # one group
        self.assertEqual(len(rupcoll[1]), 3)  # three EBRuptures