def setUp(self): # sample data to serialize for the non scenario writer asset_1 = ExposureData() asset_1.asset_ref = "a1711" asset_2 = ExposureData() asset_2.asset_ref = "a1712" asset_3 = ExposureData() asset_3.asset_ref = "a1713" self.non_scenario_mdata = { 'nrmlID': 'test_nrml_id', 'riskResultID': 'test_rr_id', 'lossMapID': 'test_lm_id', 'endBranchLabel': 'test_ebl', 'lossCategory': 'economic_loss', 'unit': 'EUR', 'timeSpan': 1.5, 'poE': 0.5 } self.non_scenario_data = [self.non_scenario_mdata, (SITE_A, [({"value": 12621260.1168}, asset_1), ({"value": 1913748.91617}, asset_2)]), (SITE_B, [({"value": 13223148.4443}, asset_3)])] self.xml_writer = risk_output.LossMapXMLWriter( TEST_LOSS_MAP_XML_OUTPUT_PATH) self.xml_non_scn_writer = risk_output.LossMapNonScenarioXMLWriter( TEST_NON_SCN_LOSS_MAP_XML_OUTPUT_PATH)
def test_loss_map_output_writes_and_validates(self): xml_writer = \ risk_output.LossMapXMLWriter(TEST_LOSS_MAP_XML_OUTPUT_PATH) xml_writer.serialize(SAMPLE_LOSS_MAP_DATA) self.assertTrue( xml.validates_against_xml_schema(TEST_LOSS_MAP_XML_OUTPUT_PATH, NRML_SCHEMA_PATH), "NRML instance file %s does not validate against schema" % \ TEST_LOSS_MAP_XML_OUTPUT_PATH)
def execute(self): """Entry point for triggering the computation.""" LOGGER.debug("Executing deterministic risk computation.") LOGGER.debug("This will calculate mean and standard deviation loss" "values for the region defined in the job config.") tasks = [] vuln_model = \ vulnerability.load_vuln_model_from_kvs(self.job_id) epsilon_provider = risk_job.EpsilonProvider(self.params) sum_per_gmf = det.SumPerGroundMotionField(vuln_model, epsilon_provider) region_loss_map_data = [] for block_id in self.blocks_keys: LOGGER.debug("Dispatching task for block %s of %s" % (block_id, len(self.blocks_keys))) a_task = risk_job.compute_risk.delay( self.id, block_id, vuln_model=vuln_model, epsilon_provider=epsilon_provider) tasks.append(a_task) for task in tasks: task.wait() if not task.successful(): raise Exception(task.result) block_loss, block_loss_map_data = task.result # do some basic validation on our results assert block_loss is not None, "Expected a result != None" assert isinstance(block_loss, numpy.ndarray), \ "Expected a numpy array" # our result should be a 1-dimensional numpy.array of loss values sum_per_gmf.sum_losses(block_loss) region_loss_map_data.extend(block_loss_map_data) # serialize the loss map data to XML loss_map_path = os.path.join( self['BASE_PATH'], self['OUTPUT_DIR'], 'loss-map-%s.xml' % self.id) loss_map_xml_writer = risk_output.LossMapXMLWriter(loss_map_path) # Add a metadata dict in the first list position # TODO(LB): we need to define some meaningful values for the metadata # here. For now, I'm just going to leave it blank. loss_map_metadata = {} region_loss_map_data.insert(0, loss_map_metadata) loss_map_xml_writer.serialize(region_loss_map_data) # For now, just print these values. # These are not debug statements; please don't remove them! print "Mean region loss value: %s" % sum_per_gmf.mean print "Standard deviation region loss value: %s" % sum_per_gmf.stddev return [True]
def setUp(self): self.xml_writer = \ risk_output.LossMapXMLWriter(TEST_LOSS_MAP_XML_OUTPUT_PATH)