def test_loss_ratio_xml_is_correct(self): """Assert that content of serialized loss ratio curve data is correct.""" # serialize curves xml_writer = risk_output.LossRatioCurveXMLWriter( self.single_loss_ratio_curve_path) xml_writer.serialize(self.single_loss_ratio_curve) # parse curves DOM-style xml_doc = etree.parse(self.single_loss_ratio_curve_path) loaded_xml = xml_doc.getroot() poe_el_txt = loaded_xml.findtext(".//%s" % xml.RISK_POE_TAG) poe_values = [float(x) for x in poe_el_txt.strip().split()] loss_ratio_el_txt = loaded_xml.findtext( ".//%s" % xml.RISK_LOSS_RATIO_ABSCISSA_TAG) loss_ratio_values = [float(x) \ for x in loss_ratio_el_txt.strip().split()] self.assertEqual(len(loss_ratio_values), len(TEST_LOSS_RATIO_CURVE.abscissae), "curve length mismatch") for idx, val in enumerate(TEST_LOSS_RATIO_CURVE.abscissae): self.assertAlmostEqual(val, float(loss_ratio_values[idx]), 6) for idx, val in enumerate(TEST_LOSS_RATIO_CURVE.ordinates): self.assertAlmostEqual(val, float(poe_values[idx]), 6)
def _serialize(path, **kwargs): """ Serialize the curves """ LOG.debug("Serializing %s" % kwargs['curve_mode']) # TODO(JMC): Take mean or max for each site if kwargs["curve_mode"] == "loss_ratio": output_generator = risk_output.LossRatioCurveXMLWriter(path) elif kwargs["curve_mode"] == 'loss': output_generator = risk_output.LossCurveXMLWriter(path) output_generator.serialize(kwargs['curves']) return path
def test_loss_ratio_is_serialized_to_file_and_validates(self): """Serialize loss ratio curve to NRML and validate against schema.""" xml_writer = risk_output.LossRatioCurveXMLWriter( self.loss_ratio_curve_path) xml_writer.serialize(self.loss_ratio_curves) self.assertTrue(xml.validates_against_xml_schema( self.loss_ratio_curve_path, NRML_SCHEMA_PATH), "NRML instance file %s does not validate against schema" % \ self.loss_ratio_curve_path)