예제 #1
0
    def test_dmg_per_asset_node(self):
        point1 = writers.Site(-116., 41.)
        point2 = writers.Site(-117., 42.)

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

        data = itertools.starmap(DMG_DIST_PER_ASSET, [
            (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),
        ])
        check_equal(__file__, 'expected_dmg_per_asset.xml',
                    dw.to_nrml('dmg_dist_per_asset', data))
예제 #2
0
    def test_serialize_an_insured_loss_curve(self):
        expected = io.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">
  <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=writers.Site(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=writers.Site(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)
예제 #3
0
    def test_serialize_a_model(self):
        expected = io.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">
  <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=writers.Site(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=writers.Site(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=writers.Site(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)
예제 #4
0
    def test_serialize_using_hazard_realization(self):
        expected = io.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">
  <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=writers.Site(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)
예제 #5
0
    def test_serialize_using_hazard_realization_geojson(self):
        expected = {
            u'features': [
                {u'geometry': {u'coordinates': [1.0, 1.5], u'type': u'Point'},
                 u'properties': {u'asset_ref': u'asset_1',
                                 u'loss': 15.23,
                                 u'std_dev': 2.0},
                 u'type': u'Feature'}],
            u'oqmetadata': {u'gsimTreePath': u'b3|b4',
                            u'investigationTime': u'10.0',
                            u'lossCategory': u'economic',
                            u'lossType': u'structural',
                            u'poE': u'0.8',
                            u'sourceModelTreePath': u'b1|b2',
                            u'unit': u'USD'},
            u'oqnrmlversion': u'0.4',
            u'oqtype': u'LossMap',
            u'type': u'FeatureCollection'}

        writer = writers.LossMapGeoJSONWriter(
            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=writers.Site(1.0, 1.5),
            value=15.23, std_dev=2)]

        writer.serialize(data)
        actual = json.load(open(self.filename))
        self.assertEqual(expected, actual)
예제 #6
0
    def test_serialize_using_hazard_realization_xml(self):
        expected = io.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">
  <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=writers.Site(1.0, 1.5),
                          value=15.23, std_dev=None)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
예제 #7
0
    def test_serialize_optional_metadata_geojson(self):
        expected = {
            u'features': [
                {u'geometry': {u'coordinates': [1.0, 1.5], u'type': u'Point'},
                 u'properties': {u'asset_ref': u'asset_1',
                                 u'loss': 15.23,
                                 u'std_dev': 2.0},
                 u'type': u'Feature'}],
            u'oqmetadata': {u'investigationTime': u'10.0',
                            u'lossCategory': u'economic',
                            u'lossType': u'structural',
                            u'poE': u'0.8',
                            u'quantileValue': u'0.5',
                            u'statistics': u'quantile',
                            u'unit': u'USD'},
            u'oqnrmlversion': u'0.4',
            u'oqtype': u'LossMap',
            u'type': u'FeatureCollection'}

        writer = writers.LossMapGeoJSONWriter(
            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=writers.Site(1.0, 1.5),
            value=15.23, std_dev=2)]

        writer.serialize(data)
        actual = json.load(open(self.filename))
        self.assertEqual(expected, actual)
예제 #8
0
    def test_serialize_optional_metadata_xml(self):
        expected = io.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">
  <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=writers.Site(1.0, 1.5),
            value=15.23, std_dev=2)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
예제 #9
0
    def test_serialize_statistics_metadata(self):
        expected = io.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">
  <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.400 0.600 1.800</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=writers.Site(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)
예제 #10
0
    def test_collapse_map_node(self):
        point1 = writers.Site(-72.2, 18.)
        point2 = writers.Site(-72.25, 18.)

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

        data = itertools.starmap(COLLAPSE_MAP, [
            (e1, 1.0, 1.6),
            (e2, 34.8, 18.3),
            (e3, 64.2, 19.8),
            (e4, 64.3, 19.7),
        ])
        check_equal(__file__, 'expected_collapse_map.xml',
                    dw.to_nrml('collapse_map', data))
예제 #11
0
class LossMapWriterTestCase(unittest.TestCase):
    """
    Tests for the XML and GeoJSON loss map writers.
    """

    filename = "loss_map.dat"
    tearDown = remove_file
    data = [
        LOSS_NODE(asset_ref="asset_1",
                  location=writers.Site(1.0, 1.5),
                  value=15.23,
                  std_dev=None),
        LOSS_NODE(asset_ref="asset_2",
                  location=writers.Site(1.0, 1.5),
                  value=16.23,
                  std_dev=None),
        LOSS_NODE(asset_ref="asset_3",
                  location=writers.Site(2.0, 2.5),
                  value=17.23,
                  std_dev=None),
    ]

    def test_empty_model_not_supported_xml(self):
        writer = writers.LossMapXMLWriter(self.filename,
                                          investigation_time=10.0,
                                          poe=0.5,
                                          statistics="mean",
                                          loss_type="structural")

        self.assertRaises(ValueError, writer.serialize, [])
        self.assertRaises(ValueError, writer.serialize, None)

    def test_empty_model_not_supported_geojson(self):
        writer = writers.LossMapGeoJSONWriter(self.filename,
                                              investigation_time=10.0,
                                              poe=0.5,
                                              statistics="mean",
                                              loss_type="structural")

        self.assertRaises(ValueError, writer.serialize, [])
        self.assertRaises(ValueError, writer.serialize, None)

    def test_serialize_a_model_xml(self):
        expected = io.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">
  <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)

    maxDiff = None

    def test_serialize_a_model_geojson(self):
        expected = {
            u'features': [{
                u'geometry': {
                    u'coordinates': [1.0, 1.5],
                    u'type': u'Point'
                },
                u'properties': {
                    u'asset_ref': u'asset_1',
                    u'loss': 15.23
                },
                u'type': u'Feature'
            }, {
                u'geometry': {
                    u'coordinates': [1.0, 1.5],
                    u'type': u'Point'
                },
                u'properties': {
                    u'asset_ref': u'asset_2',
                    u'loss': 16.23
                },
                u'type': u'Feature'
            }, {
                u'geometry': {
                    u'coordinates': [2.0, 2.5],
                    u'type': u'Point'
                },
                u'properties': {
                    u'asset_ref': u'asset_3',
                    u'loss': 17.23
                },
                u'type': u'Feature'
            }],
            u'oqmetadata': {
                u'investigationTime': u'10.0',
                u'lossType': u'structural',
                u'poE': u'0.8',
                u'statistics': u'mean'
            },
            u'oqnrmlversion':
            u'0.4',
            u'oqtype':
            u'LossMap',
            u'type':
            u'FeatureCollection'
        }
        writer = writers.LossMapGeoJSONWriter(self.filename,
                                              investigation_time=10.0,
                                              poe=0.8,
                                              statistics="mean",
                                              loss_type="structural")

        writer.serialize(self.data)

        actual = json.load(open(self.filename))
        self.assertEqual(expected, actual)

    def test_serialize_optional_metadata_xml(self):
        expected = io.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">
  <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=writers.Site(1.0, 1.5),
                      value=15.23,
                      std_dev=2)
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)

    def test_serialize_optional_metadata_geojson(self):
        expected = {
            u'features': [{
                u'geometry': {
                    u'coordinates': [1.0, 1.5],
                    u'type': u'Point'
                },
                u'properties': {
                    u'asset_ref': u'asset_1',
                    u'loss': 15.23,
                    u'std_dev': 2.0
                },
                u'type': u'Feature'
            }],
            u'oqmetadata': {
                u'investigationTime': u'10.0',
                u'lossCategory': u'economic',
                u'lossType': u'structural',
                u'poE': u'0.8',
                u'quantileValue': u'0.5',
                u'statistics': u'quantile',
                u'unit': u'USD'
            },
            u'oqnrmlversion':
            u'0.4',
            u'oqtype':
            u'LossMap',
            u'type':
            u'FeatureCollection'
        }

        writer = writers.LossMapGeoJSONWriter(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=writers.Site(1.0, 1.5),
                      value=15.23,
                      std_dev=2)
        ]

        writer.serialize(data)
        actual = json.load(open(self.filename))
        self.assertEqual(expected, actual)

    def test_serialize_using_hazard_realization_xml(self):
        expected = io.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">
  <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=writers.Site(1.0, 1.5),
                      value=15.23,
                      std_dev=None)
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)

    def test_serialize_using_hazard_realization_geojson(self):
        expected = {
            u'features': [{
                u'geometry': {
                    u'coordinates': [1.0, 1.5],
                    u'type': u'Point'
                },
                u'properties': {
                    u'asset_ref': u'asset_1',
                    u'loss': 15.23,
                    u'std_dev': 2.0
                },
                u'type': u'Feature'
            }],
            u'oqmetadata': {
                u'gsimTreePath': u'b3|b4',
                u'investigationTime': u'10.0',
                u'lossCategory': u'economic',
                u'lossType': u'structural',
                u'poE': u'0.8',
                u'sourceModelTreePath': u'b1|b2',
                u'unit': u'USD'
            },
            u'oqnrmlversion':
            u'0.4',
            u'oqtype':
            u'LossMap',
            u'type':
            u'FeatureCollection'
        }

        writer = writers.LossMapGeoJSONWriter(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=writers.Site(1.0, 1.5),
                      value=15.23,
                      std_dev=2)
        ]

        writer.serialize(data)
        actual = json.load(open(self.filename))
        self.assertEqual(expected, actual)