Пример #1
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)
Пример #2
0
    def test_empty_model_not_supported(self):
        writer = writers.LossCurveXMLWriter(
            self.filename, investigation_time=10.0, statistics="mean",
            loss_type="structural")

        self.assertRaises(ValueError, writer.serialize, [])
        self.assertRaises(ValueError, writer.serialize, None)
Пример #3
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)
Пример #4
0
def export_loss_curve_xml(key, output, target):
    """
    Export `output` to `target` by using a nrml loss curves
    serializer
    """
    args = _export_common(output, output.loss_curve.loss_type)
    dest = _get_result_export_dest(target, output)
    args['insured'] = output.loss_curve.insured

    data = output.loss_curve.losscurvedata_set.all().order_by('asset_ref')
    risk_writers.LossCurveXMLWriter(dest, **args).serialize(data)
    return dest