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"> <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") 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))
def test_empty_model_not_supported(self): writer = writers.BCRMapXMLWriter( self.filename, interest_rate=10.0, asset_life_expectancy=0.5, statistics="mean") self.assertRaises(ValueError, writer.serialize, []) self.assertRaises(ValueError, writer.serialize, None)
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))
def export_bcr_distribution_xml(output, target): """ Export `output` to `target` by using a nrml bcr distribution serializer """ risk_calculation = output.oq_job.risk_calculation args = _export_common(output, output.bcr_distribution.loss_type) dest = _get_result_export_dest(target, output) args.update( dict(interest_rate=risk_calculation.interest_rate, asset_life_expectancy=risk_calculation.asset_life_expectancy)) del args['investigation_time'] writers.BCRMapXMLWriter(dest, **args).serialize( output.bcr_distribution.bcrdistributiondata_set.all().order_by( 'asset_ref')) return dest
def test_serialize_optional_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"> <bcrMap interestRate="10.0" assetLifeExpectancy="50.0" statistics="quantile" quantileValue="0.5" 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, statistics="quantile", quantile_value=0.50, 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))