Пример #1
0
    def test_serialize(self):
        expected = StringIO.StringIO('''<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <totalDmgDist>
    <damageStates>no_damage slight moderate extensive complete</damageStates>
    <damage ds="no_damage" mean="1.0" stddev="1.6"/>
    <damage ds="slight" mean="34.8" stddev="18.3"/>
    <damage ds="moderate" mean="64.2" stddev="19.8"/>
    <damage ds="extensive" mean="64.3" stddev="19.7"/>
    <damage ds="complete" mean="64.3" stddev="19.7"/>
  </totalDmgDist>
</nrml>''')

        damage_states = 'no_damage slight moderate extensive complete'.split()
        writer = writers.DmgDistTotalXMLWriter(self.filename, damage_states)
        data = [
            (NO_DAMAGE, 1.0, 1.6),
            (SLIGHT, 34.8, 18.3),
            (MODERATE, 64.2, 19.8),
            (EXTENSIVE, 64.3, 19.7),
            (COMPLETE, 64.3, 19.7),
        ]
        writer.serialize(_starmap(DMG_DIST_TOTAL, data))

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #2
0
    def test_serialize_a_model(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml
  xmlns:gml="http://www.opengis.net/gml"
  xmlns="http://openquake.org/xmlns/nrml/0.4">
  <aggregateLossCurve
    investigationTime="10.0"
    sourceModelTreePath="b1_b2_b3"
    gsimTreePath="b1_b2"
    unit="USD"
    lossType="structural">
    <poEs>1.0 0.5 0.1</poEs>
    <losses>10.0000 20.0000 30.0000</losses>
    <averageLoss>3.0000e+00</averageLoss>
    <stdDevLoss>5.0000e-01</stdDevLoss>
  </aggregateLossCurve>
</nrml>
""")

        writer = writers.AggregateLossCurveXMLWriter(
            self.filename,
            investigation_time=10.0, source_model_tree_path="b1_b2_b3",
            gsim_tree_path="b1_b2", unit="USD", loss_type="structural")

        data = AGGREGATE_LOSS_CURVE(
            poes=[1.0, 0.5, 0.1], losses=[10.0, 20.0, 30.0],
            average_loss=3., stddev_loss=0.5)

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #3
0
    def test_serialize_a_model_xml(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8" statistics="mean" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" value="15.23"/>
      <loss assetRef="asset_2" value="16.23"/>
    </node>
    <node>
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_3" value="17.23"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(
            self.filename, investigation_time=10.0, poe=0.8,
            statistics="mean", loss_type="structural"
        )

        writer.serialize(self.data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #4
0
    def test_serialize_statistics_metadata(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml
  xmlns:gml="http://www.opengis.net/gml"
  xmlns="http://openquake.org/xmlns/nrml/0.4">
  <aggregateLossCurve
    investigationTime="10.0"
    statistics="quantile"
    quantileValue="0.5"
    lossType="structural">
    <poEs>1.0 0.5 0.1</poEs>
    <losses>10.0000 20.0000 30.0000</losses>
    <averageLoss>2.0000e+00</averageLoss>
    <stdDevLoss>5.0000e-01</stdDevLoss>
  </aggregateLossCurve>
</nrml>
""")

        writer = writers.AggregateLossCurveXMLWriter(
            self.filename,
            investigation_time=10.0, statistics="quantile",
            quantile_value=0.50, loss_type="structural")

        data = AGGREGATE_LOSS_CURVE(
            poes=[1.0, 0.5, 0.1], losses=[10.0, 20.0, 30.0],
            average_loss=2., stddev_loss=0.5)

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #5
0
    def test_serialize_taxonomies(self):
        expected = file(
            os.path.join(os.path.dirname(__file__),
                         "../../../../examples/loss-fractions-taxonomies.xml"))

        writers.LossFractionsWriter(
            self.filename,
            "taxonomy",
            loss_unit="EUR",
            loss_category="building",
            hazard_metadata=HazardMetadata(investigation_time=50.,
                                           statistics=None,
                                           quantile=None,
                                           sm_path="b1_b2_b4",
                                           gsim_path="b1_b2"),
            poe=0.1,
            loss_type="structural").serialize(
                dict(RC=(400, 0.2), RM=(1600, 0.8)), {
                    (0., 0.): dict(RC=(200, 0.5), RM=(200, 0.5)),
                    (1., 1.): dict(RC=(200, 0.25), RM=(1400, 0.75))
                })

        etree.XMLSchema(etree.parse(nrmllib.nrml_schema_file())).assert_(
            etree.parse(self.filename))
        _utils.assert_xml_equal(expected, self.filename)
Пример #6
0
    def test_serialize_using_hazard_realization_xml(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8"
           sourceModelTreePath="b1|b2" gsimTreePath="b3|b4"
           lossCategory="economic" unit="USD" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" value="15.23"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(
            self.filename,
            investigation_time=10.0, poe=0.80, source_model_tree_path="b1|b2",
            gsim_tree_path="b3|b4", unit="USD", loss_category="economic",
            loss_type="structural")

        data = [LOSS_NODE(asset_ref="asset_1",
                          location=Point(1.0, 1.5), value=15.23, std_dev=None)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #7
0
    def test_serialize_optional_metadata_xml(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8" statistics="quantile"
        quantileValue="0.5" lossCategory="economic" unit="USD" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" mean="15.23" stdDev="2"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(
            self.filename,
            investigation_time=10.0, poe=0.80, statistics="quantile",
            quantile_value=0.50, unit="USD", loss_category="economic",
            loss_type="structural"
        )

        data = [LOSS_NODE(
                asset_ref="asset_1", location=Point(1.0, 1.5), value=15.23,
                std_dev=2)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #8
0
    def test_serialize_statistics_metadata(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml
  xmlns:gml="http://www.opengis.net/gml"
  xmlns="http://openquake.org/xmlns/nrml/0.4">
  <aggregateLossCurve
    investigationTime="10.0"
    statistics="quantile"
    quantileValue="0.5"
    lossType="structural">
    <poEs>1.0 0.5 0.1</poEs>
    <losses>10.0000 20.0000 30.0000</losses>
    <averageLoss>2.0000e+00</averageLoss>
    <stdDevLoss>5.0000e-01</stdDevLoss>
  </aggregateLossCurve>
</nrml>
""")

        writer = writers.AggregateLossCurveXMLWriter(self.filename,
                                                     investigation_time=10.0,
                                                     statistics="quantile",
                                                     quantile_value=0.50,
                                                     loss_type="structural")

        data = AGGREGATE_LOSS_CURVE(poes=[1.0, 0.5, 0.1],
                                    losses=[10.0, 20.0, 30.0],
                                    average_loss=2.,
                                    stddev_loss=0.5)

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #9
0
    def test_serialize_a_model_xml(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8" statistics="mean" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" value="15.23"/>
      <loss assetRef="asset_2" value="16.23"/>
    </node>
    <node>
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_3" value="17.23"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(self.filename,
                                          investigation_time=10.0,
                                          poe=0.8,
                                          statistics="mean",
                                          loss_type="structural")

        writer.serialize(self.data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #10
0
    def test_serialize_using_hazard_realization(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <bcrMap interestRate="10.0" assetLifeExpectancy="50.0" sourceModelTreePath="b1|b2" gsimTreePath="b1|b2" lossCategory="economic" unit="USD" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_1" ratio="15.23" aalOrig="10.5" aalRetr="20.5"/>
    </node>
  </bcrMap>
</nrml>
""")

        writer = writers.BCRMapXMLWriter(
            self.filename,
            interest_rate=10.0, asset_life_expectancy=50.0,
            source_model_tree_path="b1|b2", gsim_tree_path="b1|b2",
            unit="USD", loss_category="economic", loss_type="structural")

        data = [BCR_NODE(
                asset_ref="asset_1", location=Point(1.0, 1.5),
                bcr=15.23, average_annual_loss_original=10.5,
                average_annual_loss_retrofitted=20.5)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #11
0
    def test_serialize(self):
        expected = StringIO.StringIO('''<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <totalDmgDist>
    <damageStates>no_damage slight moderate extensive complete</damageStates>
    <damage ds="no_damage" mean="1.0" stddev="1.6"/>
    <damage ds="slight" mean="34.8" stddev="18.3"/>
    <damage ds="moderate" mean="64.2" stddev="19.8"/>
    <damage ds="extensive" mean="64.3" stddev="19.7"/>
    <damage ds="complete" mean="64.3" stddev="19.7"/>
  </totalDmgDist>
</nrml>''')

        damage_states = 'no_damage slight moderate extensive complete'.split()
        writer = writers.DmgDistTotalXMLWriter(self.filename, damage_states)
        data = [
            (NO_DAMAGE, 1.0, 1.6),
            (SLIGHT, 34.8, 18.3),
            (MODERATE, 64.2, 19.8),
            (EXTENSIVE, 64.3, 19.7),
            (COMPLETE, 64.3, 19.7),
        ]
        writer.serialize(_starmap(DMG_DIST_TOTAL, data))

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #12
0
    def test_serialize_statistics_metadata(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossCurves investigationTime="10.0"
              statistics="quantile" quantileValue="0.5" lossType="structural">
    <lossCurve assetRef="asset_1">
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.5 0.1</poEs>
      <losses>10.0 20.0 30.0</losses>
      <lossRatios>0.4 0.6 1.8</lossRatios>
      <averageLoss>0.0000e+00</averageLoss>
      <stdDevLoss>9.0000e-01</stdDevLoss>
    </lossCurve>
  </lossCurves>
</nrml>
""")

        writer = writers.LossCurveXMLWriter(
            self.filename,
            investigation_time=10.0, statistics="quantile",
            quantile_value=0.50, loss_type="structural")

        data = [LOSS_CURVE(
                asset_ref="asset_1", location=Point(1.0, 1.5),
                poes=[1.0, 0.5, 0.1], losses=[10.0, 20.0, 30.0],
                loss_ratios=[0.4, 0.6, 1.8], average_loss=0., stddev_loss=0.9)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #13
0
    def test_serialize_an_insured_loss_curve(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossCurves  insured="True" investigationTime="10.0"
    sourceModelTreePath="b1_b2_b3" gsimTreePath="b1_b2" unit="USD" lossType="structural">
    <lossCurve assetRef="asset_1">
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.5 0.1</poEs>
      <losses>10.0 20.0 30.0</losses>
      <averageLoss>1.0000e+00</averageLoss>
      <stdDevLoss>5.0000e-01</stdDevLoss>
    </lossCurve>
    <lossCurve assetRef="asset_2">
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.3 0.2</poEs>
      <losses>20.0 30.0 40.0</losses>
      <averageLoss>2.0000e+00</averageLoss>
      <stdDevLoss>1.0000e-01</stdDevLoss>
    </lossCurve>
  </lossCurves>
</nrml>
""")

        writer = writers.LossCurveXMLWriter(self.filename,
                                            investigation_time=10.0,
                                            source_model_tree_path="b1_b2_b3",
                                            gsim_tree_path="b1_b2",
                                            unit="USD",
                                            insured=True,
                                            loss_type="structural")

        data = [
            LOSS_CURVE(asset_ref="asset_1",
                       location=Point(1.0, 1.5),
                       poes=[1.0, 0.5, 0.1],
                       losses=[10.0, 20.0, 30.0],
                       loss_ratios=None,
                       average_loss=1.,
                       stddev_loss=0.5),
            LOSS_CURVE(asset_ref="asset_2",
                       location=Point(2.0, 2.5),
                       poes=[1.0, 0.3, 0.2],
                       losses=[20.0, 30.0, 40.0],
                       loss_ratios=None,
                       average_loss=2.,
                       stddev_loss=0.1),
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #14
0
    def test_serialize_an_insured_loss_curve(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossCurves  insured="True" investigationTime="10.0"
    sourceModelTreePath="b1_b2_b3" gsimTreePath="b1_b2" unit="USD" lossType="structural">
    <lossCurve assetRef="asset_1">
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.5 0.1</poEs>
      <losses>10.0 20.0 30.0</losses>
      <averageLoss>1.0000e+00</averageLoss>
      <stdDevLoss>5.0000e-01</stdDevLoss>
    </lossCurve>
    <lossCurve assetRef="asset_2">
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.3 0.2</poEs>
      <losses>20.0 30.0 40.0</losses>
      <averageLoss>2.0000e+00</averageLoss>
      <stdDevLoss>1.0000e-01</stdDevLoss>
    </lossCurve>
  </lossCurves>
</nrml>
""")

        writer = writers.LossCurveXMLWriter(
            self.filename,
            investigation_time=10.0, source_model_tree_path="b1_b2_b3",
            gsim_tree_path="b1_b2", unit="USD", insured=True,
            loss_type="structural")

        data = [
            LOSS_CURVE(
                asset_ref="asset_1", location=Point(1.0, 1.5),
                poes=[1.0, 0.5, 0.1], losses=[10.0, 20.0, 30.0],
                loss_ratios=None, average_loss=1., stddev_loss=0.5),

            LOSS_CURVE(
                asset_ref="asset_2", location=Point(2.0, 2.5),
                poes=[1.0, 0.3, 0.2], losses=[20.0, 30.0, 40.0],
                loss_ratios=None, average_loss=2., stddev_loss=0.1),
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #15
0
    def test_serialize_a_model(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <bcrMap interestRate="10.0" assetLifeExpectancy="50.0" statistics="mean" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_1" ratio="15.23" aalOrig="10.5" aalRetr="20.5"/>
      <bcr assetRef="asset_2" ratio="16.23" aalOrig="11.5" aalRetr="40.5"/>
    </node>
    <node>
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_3" ratio="17.23" aalOrig="12.5" aalRetr="10.5"/>
    </node>
  </bcrMap>
</nrml>
""")

        writer = writers.BCRMapXMLWriter(self.filename,
                                         interest_rate=10.0,
                                         asset_life_expectancy=50.0,
                                         statistics="mean",
                                         loss_type="structural")

        data = [
            BCR_NODE(asset_ref="asset_1",
                     location=Point(1.0, 1.5),
                     bcr=15.23,
                     average_annual_loss_original=10.5,
                     average_annual_loss_retrofitted=20.5),
            BCR_NODE(asset_ref="asset_2",
                     location=Point(1.0, 1.5),
                     bcr=16.23,
                     average_annual_loss_original=11.5,
                     average_annual_loss_retrofitted=40.5),
            BCR_NODE(asset_ref="asset_3",
                     location=Point(2.0, 2.5),
                     bcr=17.23,
                     average_annual_loss_original=12.5,
                     average_annual_loss_retrofitted=10.5),
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #16
0
    def test_chain_parse_serialize(self):
        # Chain a parser together to with a serializer and test that the
        # produced XML is unchanged.
        for example in ('hazard-curves-mean.xml', 'hazard-curves-pga.xml',
                        'hazard-curves-quantile.xml', 'hazard-curves-sa.xml'):
            infile = os.path.join(DATADIR, example)
            hcp = parsers.HazardCurveXMLParser(infile)
            parsed_model = hcp.parse()
            _, outfile = tempfile.mkstemp()
            try:
                hcw = writers.HazardCurveXMLWriter(outfile,
                                                   **parsed_model.metadata)
                hcw.serialize(parsed_model)

                _utils.assert_xml_equal(infile, outfile)
            finally:
                os.unlink(outfile)
Пример #17
0
    def test_serialize_a_model(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <bcrMap interestRate="10.0" assetLifeExpectancy="50.0" statistics="mean" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_1" ratio="15.23" aalOrig="10.5" aalRetr="20.5"/>
      <bcr assetRef="asset_2" ratio="16.23" aalOrig="11.5" aalRetr="40.5"/>
    </node>
    <node>
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_3" ratio="17.23" aalOrig="12.5" aalRetr="10.5"/>
    </node>
  </bcrMap>
</nrml>
""")

        writer = writers.BCRMapXMLWriter(
            self.filename,
            interest_rate=10.0, asset_life_expectancy=50.0, statistics="mean",
            loss_type="structural")

        data = [
            BCR_NODE(
                asset_ref="asset_1", location=Point(1.0, 1.5),
                bcr=15.23, average_annual_loss_original=10.5,
                average_annual_loss_retrofitted=20.5),
            BCR_NODE(
                asset_ref="asset_2", location=Point(1.0, 1.5),
                bcr=16.23, average_annual_loss_original=11.5,
                average_annual_loss_retrofitted=40.5),
            BCR_NODE(
                asset_ref="asset_3", location=Point(2.0, 2.5),
                bcr=17.23, average_annual_loss_original=12.5,
                average_annual_loss_retrofitted=10.5),
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #18
0
    def test_chain_parse_serialize(self):
        # Chain a parser together to with a serializer and test that the
        # produced XML is unchanged.
        for example in ('hazard-curves-mean.xml', 'hazard-curves-pga.xml',
                        'hazard-curves-quantile.xml', 'hazard-curves-sa.xml'):
            infile = os.path.join(DATADIR, example)
            hcp = parsers.HazardCurveXMLParser(infile)
            parsed_model = hcp.parse()
            _, outfile = tempfile.mkstemp()
            try:
                hcw = writers.HazardCurveXMLWriter(
                    outfile, **parsed_model.metadata
                )
                hcw.serialize(parsed_model)

                _utils.assert_xml_equal(infile, outfile)
            finally:
                os.unlink(outfile)
Пример #19
0
    def test_serialize_statistics_metadata(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossCurves investigationTime="10.0"
              statistics="quantile" quantileValue="0.5" lossType="structural">
    <lossCurve assetRef="asset_1">
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.5 0.1</poEs>
      <losses>10.0 20.0 30.0</losses>
      <lossRatios>0.4 0.6 1.8</lossRatios>
      <averageLoss>0.0000e+00</averageLoss>
      <stdDevLoss>9.0000e-01</stdDevLoss>
    </lossCurve>
  </lossCurves>
</nrml>
""")

        writer = writers.LossCurveXMLWriter(self.filename,
                                            investigation_time=10.0,
                                            statistics="quantile",
                                            quantile_value=0.50,
                                            loss_type="structural")

        data = [
            LOSS_CURVE(asset_ref="asset_1",
                       location=Point(1.0, 1.5),
                       poes=[1.0, 0.5, 0.1],
                       losses=[10.0, 20.0, 30.0],
                       loss_ratios=[0.4, 0.6, 1.8],
                       average_loss=0.,
                       stddev_loss=0.9)
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #20
0
    def test_serialize(self):
        expected = StringIO.StringIO('''<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <collapseMap>
    <CMNode>
      <gml:Point>
        <gml:pos>-72.2 18.0</gml:pos>
      </gml:Point>
      <cf assetRef="a1" mean="1.0" stdDev="1.6"/>
      <cf assetRef="a2" mean="34.8" stdDev="18.3"/>
      <cf assetRef="a3" mean="64.2" stdDev="19.8"/>
    </CMNode>
    <CMNode>
      <gml:Point>
        <gml:pos>-72.25 18.0</gml:pos>
      </gml:Point>
      <cf assetRef="a4" mean="64.3" stdDev="19.7"/>
    </CMNode>
  </collapseMap>
</nrml>
''')
        writer = writers.CollapseMapXMLWriter(self.filename)

        point1 = Point(-72.2, 18.)
        point2 = Point(-72.25, 18.)

        e1 = ExposureData('a1', point1)
        e2 = ExposureData('a2', point1)
        e3 = ExposureData('a3', point1)
        e4 = ExposureData('a4', point2)

        data = [
            (e1, 1.0, 1.6),
            (e2, 34.8, 18.3),
            (e3, 64.2, 19.8),
            (e4, 64.3, 19.7),
        ]
        writer.serialize(_starmap(COLLAPSE_MAP, data))
        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #21
0
    def test_serialize(self):
        expected = StringIO.StringIO('''<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <collapseMap>
    <CMNode>
      <gml:Point>
        <gml:pos>-72.2 18.0</gml:pos>
      </gml:Point>
      <cf assetRef="a1" mean="1.0" stdDev="1.6"/>
      <cf assetRef="a2" mean="34.8" stdDev="18.3"/>
      <cf assetRef="a3" mean="64.2" stdDev="19.8"/>
    </CMNode>
    <CMNode>
      <gml:Point>
        <gml:pos>-72.25 18.0</gml:pos>
      </gml:Point>
      <cf assetRef="a4" mean="64.3" stdDev="19.7"/>
    </CMNode>
  </collapseMap>
</nrml>
''')
        writer = writers.CollapseMapXMLWriter(self.filename)

        point1 = Point(-72.2, 18.)
        point2 = Point(-72.25, 18.)

        e1 = ExposureData('a1', point1)
        e2 = ExposureData('a2', point1)
        e3 = ExposureData('a3', point1)
        e4 = ExposureData('a4', point2)

        data = [
            (e1, 1.0, 1.6),
            (e2, 34.8, 18.3),
            (e3, 64.2, 19.8),
            (e4, 64.3, 19.7),
        ]
        writer.serialize(_starmap(COLLAPSE_MAP, data))
        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #22
0
    def test_serialize_using_hazard_realization_xml(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8"
           sourceModelTreePath="b1|b2" gsimTreePath="b3|b4"
           lossCategory="economic" unit="USD" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" value="15.23"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(self.filename,
                                          investigation_time=10.0,
                                          poe=0.80,
                                          source_model_tree_path="b1|b2",
                                          gsim_tree_path="b3|b4",
                                          unit="USD",
                                          loss_category="economic",
                                          loss_type="structural")

        data = [
            LOSS_NODE(asset_ref="asset_1",
                      location=Point(1.0, 1.5),
                      value=15.23,
                      std_dev=None)
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #23
0
    def test_serialize_a_model(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml
  xmlns:gml="http://www.opengis.net/gml"
  xmlns="http://openquake.org/xmlns/nrml/0.4">
  <aggregateLossCurve
    investigationTime="10.0"
    sourceModelTreePath="b1_b2_b3"
    gsimTreePath="b1_b2"
    unit="USD"
    lossType="structural">
    <poEs>1.0 0.5 0.1</poEs>
    <losses>10.0000 20.0000 30.0000</losses>
    <averageLoss>3.0000e+00</averageLoss>
    <stdDevLoss>5.0000e-01</stdDevLoss>
  </aggregateLossCurve>
</nrml>
""")

        writer = writers.AggregateLossCurveXMLWriter(
            self.filename,
            investigation_time=10.0,
            source_model_tree_path="b1_b2_b3",
            gsim_tree_path="b1_b2",
            unit="USD",
            loss_type="structural")

        data = AGGREGATE_LOSS_CURVE(poes=[1.0, 0.5, 0.1],
                                    losses=[10.0, 20.0, 30.0],
                                    average_loss=3.,
                                    stddev_loss=0.5)

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #24
0
    def test_serialize_optional_metadata_xml(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8" statistics="quantile"
        quantileValue="0.5" lossCategory="economic" unit="USD" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" mean="15.23" stdDev="2"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(self.filename,
                                          investigation_time=10.0,
                                          poe=0.80,
                                          statistics="quantile",
                                          quantile_value=0.50,
                                          unit="USD",
                                          loss_category="economic",
                                          loss_type="structural")

        data = [
            LOSS_NODE(asset_ref="asset_1",
                      location=Point(1.0, 1.5),
                      value=15.23,
                      std_dev=2)
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #25
0
    def test_serialize_taxonomies(self):
        expected = file(
            os.path.join(
                os.path.dirname(__file__),
                "../../../../examples/loss-fractions-taxonomies.xml"))

        writers.LossFractionsWriter(
            self.filename, "taxonomy",
            loss_unit="EUR",
            loss_category="building",
            hazard_metadata=HazardMetadata(
                investigation_time=50.,
                statistics=None,
                quantile=None,
                sm_path="b1_b2_b4",
                gsim_path="b1_b2"), poe=0.1, loss_type="structural").serialize(
                    dict(RC=(400, 0.2), RM=(1600, 0.8)),
                    {(0., 0.): dict(RC=(200, 0.5), RM=(200, 0.5)),
                     (1., 1.): dict(RC=(200, 0.25), RM=(1400, 0.75))})

        etree.XMLSchema(
            etree.parse(nrmllib.nrml_schema_file())).assert_(
                etree.parse(self.filename))
        _utils.assert_xml_equal(expected, self.filename)
Пример #26
0
    def test_serialize_using_hazard_realization(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <bcrMap interestRate="10.0" assetLifeExpectancy="50.0" sourceModelTreePath="b1|b2" gsimTreePath="b1|b2" lossCategory="economic" unit="USD" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_1" ratio="15.23" aalOrig="10.5" aalRetr="20.5"/>
    </node>
  </bcrMap>
</nrml>
""")

        writer = writers.BCRMapXMLWriter(self.filename,
                                         interest_rate=10.0,
                                         asset_life_expectancy=50.0,
                                         source_model_tree_path="b1|b2",
                                         gsim_tree_path="b1|b2",
                                         unit="USD",
                                         loss_category="economic",
                                         loss_type="structural")

        data = [
            BCR_NODE(asset_ref="asset_1",
                     location=Point(1.0, 1.5),
                     bcr=15.23,
                     average_annual_loss_original=10.5,
                     average_annual_loss_retrofitted=20.5)
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #27
0
    def test_serialize(self):
        expected = StringIO.StringIO('''<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <dmgDistPerAsset>
    <damageStates>no_damage slight moderate extensive complete</damageStates>
    <DDNode>
      <gml:Point>
        <gml:pos>-116.0 41.0</gml:pos>
      </gml:Point>
      <asset assetRef="asset_1">
        <damage ds="no_damage" mean="1.0" stddev="1.6"/>
        <damage ds="slight" mean="34.8" stddev="18.3"/>
        <damage ds="moderate" mean="64.2" stddev="19.8"/>
        <damage ds="extensive" mean="64.3" stddev="19.7"/>
        <damage ds="complete" mean="64.3" stddev="19.7"/>
      </asset>
    </DDNode>
    <DDNode>
      <gml:Point>
        <gml:pos>-117.0 42.0</gml:pos>
      </gml:Point>
      <asset assetRef="asset_2">
        <damage ds="no_damage" mean="1.0" stddev="1.6"/>
        <damage ds="slight" mean="34.8" stddev="18.3"/>
        <damage ds="moderate" mean="64.2" stddev="19.8"/>
        <damage ds="extensive" mean="64.3" stddev="19.7"/>
        <damage ds="complete" mean="64.3" stddev="19.7"/>
      </asset>
      <asset assetRef="asset_3">
        <damage ds="no_damage" mean="1.1" stddev="1.7"/>
        <damage ds="slight" mean="34.9" stddev="18.4"/>
        <damage ds="moderate" mean="64.2" stddev="19.8"/>
        <damage ds="extensive" mean="64.3" stddev="19.7"/>
        <damage ds="complete" mean="64.3" stddev="19.7"/>
      </asset>
    </DDNode>
  </dmgDistPerAsset>
</nrml>''')
        dmg_states = 'no_damage slight moderate extensive complete'.split()
        point1 = Point(-116., 41.)
        point2 = Point(-117., 42.)

        e1 = ExposureData('asset_1', point1)
        e2 = ExposureData('asset_2', point2)
        e3 = ExposureData('asset_3', point2)

        data = [
            (e1, NO_DAMAGE, 1.0, 1.6),
            (e1, SLIGHT, 34.8, 18.3),
            (e1, MODERATE, 64.2, 19.8),
            (e1, EXTENSIVE, 64.3, 19.7),
            (e1, COMPLETE, 64.3, 19.7),

            (e2, NO_DAMAGE, 1.0, 1.6),
            (e2, SLIGHT, 34.8, 18.3),
            (e2, MODERATE, 64.2, 19.8),
            (e2, EXTENSIVE, 64.3, 19.7),
            (e2, COMPLETE, 64.3, 19.7),

            (e3, NO_DAMAGE, 1.1, 1.7),
            (e3, SLIGHT, 34.9, 18.4),
            (e3, MODERATE, 64.2, 19.8),
            (e3, EXTENSIVE, 64.3, 19.7),
            (e3, COMPLETE, 64.3, 19.7),
        ]
        writer = writers.DmgDistPerAssetXMLWriter(self.filename, dmg_states)
        writer.serialize(_starmap(DMG_DIST_PER_ASSET, data))

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Пример #28
0
    def test_serialize(self):
        expected = StringIO.StringIO('''<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <dmgDistPerAsset>
    <damageStates>no_damage slight moderate extensive complete</damageStates>
    <DDNode>
      <gml:Point>
        <gml:pos>-116.0 41.0</gml:pos>
      </gml:Point>
      <asset assetRef="asset_1">
        <damage ds="no_damage" mean="1.0" stddev="1.6"/>
        <damage ds="slight" mean="34.8" stddev="18.3"/>
        <damage ds="moderate" mean="64.2" stddev="19.8"/>
        <damage ds="extensive" mean="64.3" stddev="19.7"/>
        <damage ds="complete" mean="64.3" stddev="19.7"/>
      </asset>
    </DDNode>
    <DDNode>
      <gml:Point>
        <gml:pos>-117.0 42.0</gml:pos>
      </gml:Point>
      <asset assetRef="asset_2">
        <damage ds="no_damage" mean="1.0" stddev="1.6"/>
        <damage ds="slight" mean="34.8" stddev="18.3"/>
        <damage ds="moderate" mean="64.2" stddev="19.8"/>
        <damage ds="extensive" mean="64.3" stddev="19.7"/>
        <damage ds="complete" mean="64.3" stddev="19.7"/>
      </asset>
      <asset assetRef="asset_3">
        <damage ds="no_damage" mean="1.1" stddev="1.7"/>
        <damage ds="slight" mean="34.9" stddev="18.4"/>
        <damage ds="moderate" mean="64.2" stddev="19.8"/>
        <damage ds="extensive" mean="64.3" stddev="19.7"/>
        <damage ds="complete" mean="64.3" stddev="19.7"/>
      </asset>
    </DDNode>
  </dmgDistPerAsset>
</nrml>''')
        dmg_states = 'no_damage slight moderate extensive complete'.split()
        point1 = Point(-116., 41.)
        point2 = Point(-117., 42.)

        e1 = ExposureData('asset_1', point1)
        e2 = ExposureData('asset_2', point2)
        e3 = ExposureData('asset_3', point2)

        data = [
            (e1, NO_DAMAGE, 1.0, 1.6),
            (e1, SLIGHT, 34.8, 18.3),
            (e1, MODERATE, 64.2, 19.8),
            (e1, EXTENSIVE, 64.3, 19.7),
            (e1, COMPLETE, 64.3, 19.7),
            (e2, NO_DAMAGE, 1.0, 1.6),
            (e2, SLIGHT, 34.8, 18.3),
            (e2, MODERATE, 64.2, 19.8),
            (e2, EXTENSIVE, 64.3, 19.7),
            (e2, COMPLETE, 64.3, 19.7),
            (e3, NO_DAMAGE, 1.1, 1.7),
            (e3, SLIGHT, 34.9, 18.4),
            (e3, MODERATE, 64.2, 19.8),
            (e3, EXTENSIVE, 64.3, 19.7),
            (e3, COMPLETE, 64.3, 19.7),
        ]
        writer = writers.DmgDistPerAssetXMLWriter(self.filename, dmg_states)
        writer.serialize(_starmap(DMG_DIST_PER_ASSET, data))

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))