def _store_em(self): [ism] = models.inputs4job(self.job.id, input_type="exposure") em = models.ExposureModel(owner=ism.owner, input=ism, name="AAA", category="single_asset", reco_type="aggregated", reco_unit="USD", stco_type="aggregated", stco_unit="USD") em.save() models.ExposureData(exposure_model=em, taxonomy="RC", asset_ref="A", number_of_units=100, stco=1, site=geos.GEOSGeometry(self.site.point.to_wkt()), reco=1).save() models.ExposureData(exposure_model=em, taxonomy="RM", asset_ref="B", number_of_units=40, stco=1, site=geos.GEOSGeometry(self.site.point.to_wkt()), reco=1).save() return em
def setUp(self): self.loss_curve_path = helpers.get_output_path(LOSS_XML_OUTPUT_FILE) self.loss_ratio_curve_path = helpers.get_output_path( LOSS_RATIO_XML_OUTPUT_FILE) self.single_loss_curve_path = helpers.get_output_path( SINGLE_LOSS_XML_OUTPUT_FILE) self.single_loss_ratio_curve_path = helpers.get_output_path( SINGLE_LOSS_RATIO_XML_OUTPUT_FILE) self.schema_path = NRML_SCHEMA_PATH # Build up some sample loss/loss ratio curves here first_site = shapes.Site(-117.0, 38.0) second_site = shapes.Site(-118.0, 39.0) first_asset = models.ExposureData(asset_ref='a1711') second_asset = models.ExposureData(asset_ref='a1712') self.loss_curves = [(first_site, (TEST_LOSS_CURVE, first_asset)), (second_site, (TEST_LOSS_CURVE, second_asset))] self.loss_ratio_curves = [ (first_site, (TEST_LOSS_RATIO_CURVE, first_asset)), (second_site, (TEST_LOSS_RATIO_CURVE, second_asset)) ] self.single_loss_curve = [(first_site, (TEST_LOSS_CURVE, first_asset))] self.single_loss_ratio_curve = [(first_site, (TEST_LOSS_RATIO_CURVE, first_asset))] # loss curve that fails with inconsistent sites for an asset self.loss_curves_fail = [(first_site, (TEST_LOSS_CURVE, first_asset)), (second_site, (TEST_LOSS_CURVE, first_asset))]
def make_assets(self): [ism] = models.inputs4job(self.job.id, input_type="exposure") em = models.ExposureModel( owner=ism.owner, input=ism, name="AAA", category="single_asset", reco_type="aggregated", reco_unit="USD", stco_type="aggregated", stco_unit="USD") em.save() site_1 = shapes.Site(-116.0, 41.0) site_2 = shapes.Site(-117.0, 42.0) asset_1 = models.ExposureData( exposure_model=em, taxonomy="RC", asset_ref="asset_1", number_of_units=100, stco=1, site=geos.GEOSGeometry(site_1.point.to_wkt()), reco=1) asset_2 = models.ExposureData( exposure_model=em, taxonomy="RM", asset_ref="asset_2", number_of_units=40, stco=1, site=geos.GEOSGeometry(site_2.point.to_wkt()), reco=1) asset_3 = models.ExposureData( exposure_model=em, taxonomy="RM", asset_ref="asset_3", number_of_units=40, stco=1, site=geos.GEOSGeometry(site_2.point.to_wkt()), reco=1) asset_1.save() asset_2.save() asset_3.save() return asset_1, asset_2, asset_3
def setUpClass(cls): default_user = helpers.default_user() cls.job = models.OqJob(owner=default_user) cls.job.save() # dmg dist per asset cls.ddpa_output = models.Output( owner=default_user, oq_job=cls.job, display_name='Test dmg dist per asset', output_type='dmg_dist_per_asset', db_backed=True) cls.ddpa_output.save() cls.ddpa = models.DmgDistPerAsset( output=cls.ddpa_output, dmg_states=cls.DMG_STATES) cls.ddpa.save() # We also need some sample exposure data records (to satisfy the dmg # dist per asset FK). test_input = models.Input( owner=default_user, input_type='exposure', path='fake', size=0) test_input.save() i2j = models.Input2job(input=test_input, oq_job=cls.job) i2j.save() exp_model = models.ExposureModel( owner=default_user, input=test_input, name='test-exp-model', category='economic loss', stco_type='per_asset', stco_unit='CHF') exp_model.save() test_site = shapes.Site(3.14, 2.17) cls.exp_data = models.ExposureData( # Asset exposure_model=exp_model, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=37, site=test_site.point.to_wkt(), stco=1234.56) cls.exp_data.save() # dmg dist per taxonomy cls.ddpt_output = models.Output( owner=default_user, oq_job=cls.job, display_name='Test dmg dist per taxonomy', output_type='dmg_dist_per_taxonomy', db_backed=True) cls.ddpt_output.save() cls.ddpt = models.DmgDistPerTaxonomy( output=cls.ddpt_output, dmg_states=cls.DMG_STATES) cls.ddpt.save() # total dmg dist cls.ddt_output = models.Output( owner=default_user, oq_job=cls.job, display_name='Test dmg dist total', output_type='dmg_dist_total', db_backed=True) cls.ddt_output.save() cls.ddt = models.DmgDistTotal( output=cls.ddt_output, dmg_states=cls.DMG_STATES) cls.ddt.save()
def test_exposure_data_with_no_number_and_coco_reco_not_aggregated(self): # the 'number_of_units' datum must be present when the contents # and retrofitting cost type is not 'aggregated'. self.mdl.area_type = "per_asset" self.mdl.area_unit = "sqm" self.mdl.reco_type = "per_area" self.mdl.reco_unit = "TJR" self.mdl.coco_type = "per_asset" self.mdl.coco_unit = "MVR" self.mdl.save() site = shapes.Site(-121.9000, 38.1000) edata = models.ExposureData(exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=16.0, coco=17.0, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: number_of_units is mandatory for " "<coco_type=per_asset, reco_type=per_area> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def setUpClass(cls): cls.job = engine.prepare_job() jp, _, _ = engine.import_job_profile(RISK_DEMO_CONFIG_FILE, cls.job) cls.job_ctxt = helpers.create_job({}, job_id=cls.job.id, oq_job_profile=jp, oq_job=cls.job) calc = ClassicalRiskCalculator(cls.job_ctxt) calc.store_exposure_assets() [input] = models.inputs4job(cls.job.id, input_type="exposure") model = input.model() assets = model.exposuredata_set.filter(taxonomy="af/ctc-D/LR") # Add some more assets. coos = [(10.000155392289116, 46.546194318563), (10.222034128255, 46.0071299176413), (10.520376165581, 46.247463385278)] for lat, lon in coos: site = shapes.Site(lat, lon) cls.sites.append(site) if assets: continue location = geos.GEOSGeometry(site.point.to_wkt()) asset = models.ExposureData(exposure_model=model, taxonomy="af/ctc-D/LR", asset_ref=helpers.random_string(6), stco=lat * 2, site=location, reco=1.1 * lon) asset.save()
def setUp(self): imls = [0.10, 0.30, 0.50, 1.00] loss_ratios = [0.05, 0.10, 0.15, 0.30] covs = [0.30, 0.30, 0.20, 0.20] self.vuln_function = shapes.VulnerabilityFunction( imls, loss_ratios, covs, "LN") self.gmvs = { "IMLs": (0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595) } self.epsilons = [ 0.5377, 1.8339, -2.2588, 0.8622, 0.3188, -1.3077, -0.4336, 0.3426, 3.5784, 2.7694 ] self.asset = models.ExposureData(exposure_model=self.emdl, stco=1000) self.eps_provider = helpers.EpsilonProvider(self.asset, self.epsilons) self.losses = numpy.array([ 72.23120833, 410.55950159, 180.02423357, 171.02684563, 250.77079384, 39.45861103, 114.54372035, 288.28653452, 473.38307021, 488.47447798 ])
def test_exposure_data_with_coco_and_unit_type_count(self): # the content cost may be present when unit_type is set to "count" site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt(), coco=112) edata.save()
def test_exposure_data_unit_type_count(self): # the unit_type is set to "count" and we can insert an `exposure_data` # row as long as we omit the area, reco and stco properties. site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt()) edata.save()
def setUp(self): path = os.path.join(helpers.SCHEMA_EXAMPLES_DIR, "LCB-exposure.yaml") inputs = [("exposure", path)] self.job = self.setup_classic_job(inputs=inputs) [input] = models.inputs4job(self.job.id, input_type="exposure", path=path) owner = models.OqUser.objects.get(user_name="openquake") emdl = input.model() if not emdl: emdl = models.ExposureModel(owner=owner, input=input, description="LCB exposure model", category="LCB cars", stco_unit="peanuts", stco_type="aggregated") emdl.save() asset_data = [ (Site(-118.077721, 33.852034), { u'stco': 5.07, u'asset_ref': u'a5625', u'taxonomy': u'HAZUS_RM1L_LC' }), (Site(-118.077721, 33.852034), { u'stco': 5.63, u'asset_ref': u'a5629', u'taxonomy': u'HAZUS_URML_LC' }), (Site(-118.077721, 33.852034), { u'stco': 11.26, u'asset_ref': u'a5630', u'taxonomy': u'HAZUS_URML_LS' }), (Site(-118.077721, 33.852034), { u'stco': 5.5, u'asset_ref': u'a5636', u'taxonomy': u'HAZUS_C3L_MC' }), ] for idx, (site, adata) in enumerate(asset_data): location = GEOSGeometry(site.point.to_wkt()) asset = models.ExposureData(exposure_model=emdl, site=location, **adata) asset.save() RISK_LOSS_CURVE_DATA[idx][1][1] = asset output_path = self.generate_output_path(self.job) self.display_name = os.path.basename(output_path) self.writer = LossCurveDBWriter(output_path, self.job.id) self.reader = LossCurveDBReader()
def setUp(self): path = os.path.join(helpers.SCHEMA_EXAMPLES_DIR, "LMB-exposure.yaml") inputs = [("exposure", path)] self.job = self.setup_classic_job(inputs=inputs) [input] = models.inputs4job(self.job.id, input_type="exposure", path=path) owner = models.OqUser.objects.get(user_name="openquake") emdl = input.model() if not emdl: emdl = models.ExposureModel(owner=owner, input=input, description="LMB exposure model", category="LMB yachts", stco_unit="oranges", stco_type="aggregated") emdl.save() asset_data = [ ("asset_a_1", SITE_A, { u'stco': 5.07, u'asset_ref': u'a1711', u'taxonomy': u'HAZUS_RM1L_LC' }), ("asset_a_2", SITE_A, { u'stco': 5.63, u'asset_ref': u'a1712', u'taxonomy': u'HAZUS_URML_LC' }), ("asset_b_1", SITE_B, { u'stco': 5.5, u'asset_ref': u'a1713', u'taxonomy': u'HAZUS_C3L_MC' }), ] for idx, (name, site, adata) in enumerate(asset_data): location = GEOSGeometry(site.point.to_wkt()) asset = models.ExposureData(exposure_model=emdl, site=location, **adata) asset.save() setattr(self, name, asset) SAMPLE_NONSCENARIO_LOSS_MAP_DATA[1][1][0][1] = self.asset_a_1 SAMPLE_NONSCENARIO_LOSS_MAP_DATA[1][1][1][1] = self.asset_a_2 SAMPLE_NONSCENARIO_LOSS_MAP_DATA[2][1][0][1] = self.asset_b_1 output_path = self.generate_output_path(self.job) self.display_name = os.path.basename(output_path) self.writer = LossMapDBWriter(output_path, self.job.id) self.reader = LossMapDBReader()
def setUpClass(cls): cls.job = engine.prepare_job() jp, _, _ = engine.import_job_profile(RISK_DEMO_CONFIG_FILE, cls.job) calc_proxy = helpers.create_job({}, job_id=cls.job.id, oq_job_profile=jp, oq_job=cls.job) # storing the basic exposure model ClassicalRiskCalculator(calc_proxy).store_exposure_assets() [input] = models.inputs4job(cls.job.id, input_type="exposure") model = input.model() assets = model.exposuredata_set.filter(taxonomy="aa/aatc-D/LR") if not assets: # This model did not exist in the database before. site = shapes.Site(1.0, 2.0) # more assets at same location models.ExposureData(exposure_model=model, taxonomy="aa/aatc-D/LR", asset_ref="ASSET_1", stco=1, site=geos.GEOSGeometry(site.point.to_wkt()), reco=1).save() models.ExposureData(exposure_model=model, taxonomy="aa/aatc-D/LR", asset_ref="ASSET_2", stco=1, site=geos.GEOSGeometry(site.point.to_wkt()), reco=1).save() site = shapes.Site(2.0, 2.0) # just one asset at location models.ExposureData(exposure_model=model, taxonomy="aa/aatc-D/LR", asset_ref="ASSET_3", stco=1, site=geos.GEOSGeometry(site.point.to_wkt()), reco=1).save()
def test_exposure_data_with_no_stco_and_population(self): # the structural cost needs not be present when we calculate exposure # in terms of population self.mdl.stco_type = None self.mdl.stco_unit = None self.mdl.category = "population" self.mdl.save() site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt()) edata.save()
def test_exposure_data_with_area_and_unit_type_count(self): # the area cost must not be present when unit_type is set to "count" site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt(), area=112) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: We are in counting mode: neither of these must " "be set ['area'] (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_data_with_coco_type_but_no_value(self): # if the content cost type is set the value must be set as well. site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: contents cost is mandatory for " "<coco_type=per_asset> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_data_with_no_stco_and_category_not_population(self): # the structural cost must be present when we calculate exposure # in terms other than population. self.mdl.save() site = shapes.Site(-122.4000, 37.6000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: structural cost is mandatory for category " "<economic loss> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_data_with_no_number_and_population(self): # the 'number_of_units' datum must be present when we calculate # exposure in terms of population self.mdl.category = "population" self.mdl.save() site = shapes.Site(-122.3000, 37.7000) edata = models.ExposureData(exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: number_of_units is mandatory for " "<category=population> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_data_with_no_number_and_stco_type_not_aggregated(self): # the 'number_of_units' datum must be present when the structural # cost type is not 'aggregated'. self.mdl.stco_type = "per_asset" self.mdl.save() site = shapes.Site(-122.2000, 37.8000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=11.0, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: number_of_units is mandatory for " "<stco_type=per_asset> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_stco_type_but_no_stco_value(self): # the structural cost must be present if the structural cost type # is set. self.mdl.category = "population" self.mdl.save() site = shapes.Site(-121.6000, 38.4000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=24, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: structural cost is mandatory for " "<stco_type=aggregated> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def insert_datum(self, point, occupancy, values): """ Insert a single asset entry. :param point: asset location :type point: :class:`openquake.shapes.Site` :param list occupancy: a potentially empty list of named tuples each having an 'occupants' and a 'description' property :param values: dictionary of values (see :class:`openquake.parser.exposure.ExposureModelFile`) it also inserts the main exposure model entry if not already present, """ if not self.model: self.model = models.ExposureModel( owner=self.owner, input=self.smi, description=values.get("listDescription"), taxonomy_source=values.get("taxonomySource"), category=values["assetCategory"]) for key, tag in self.model_attrs: value = values.get(tag) if value: setattr(self.model, key, value) self.model.save() data = models.ExposureData(exposure_model=self.model, asset_ref=values["assetID"], taxonomy=values.get("taxonomy"), site="POINT(%s %s)" % (point.point.x, point.point.y)) for key, tag in [("coco", "coco"), ("reco", "reco"), ("stco", "stco"), ("area", "area"), ("number_of_units", "number"), ("deductible", "deductible"), ("ins_limit", "limit")]: value = values.get(tag) if value: setattr(data, key, value) data.save() for odata in occupancy: oobj = models.Occupancy(exposure_data=data, occupants=odata.occupants, description=odata.description) oobj.save()
def test_exposure_coco_type_but_no_coco_value(self): # the contents cost must be present if the contents cost type # is set. self.mdl.coco_type = "per_asset" self.mdl.coco_unit = "MUR" self.mdl.save() site = shapes.Site(-121.7000, 38.3000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=19.0, number_of_units=23, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: contents cost is mandatory for " "<coco_type=per_asset> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_stco_type_per_area_but_no_area_value(self): # the area must be set if the structural cost type is 'per_area' self.mdl.stco_type = "per_area" self.mdl.stco_unit = "IDR" self.mdl.area_type = "aggregated" self.mdl.area_unit = "ATS" self.mdl.save() site = shapes.Site(-121.3000, 38.7000) edata = models.ExposureData(exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=22.0, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: area is mandatory for <stco_type=per_area> " "(exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def setUp(self): self.job_ctxt = helpers.job_from_file( os.path.join(helpers.DATA_DIR, 'config.gem')) [input] = models.inputs4job(self.job_ctxt.job_id, input_type="exposure") owner = models.OqUser.objects.get(user_name="openquake") emdl = input.model() if not emdl: emdl = models.ExposureModel(owner=owner, input=input, description="RCT exposure model", category="RCT villas", stco_unit="roofs", stco_type="aggregated") emdl.save() asset_data = [ ((0, 0), shapes.Site(10.0, 10.0), { u'stco': 5.07, u'asset_ref': u'a5625', u'taxonomy': u'rctc-ad-83' }), ((0, 1), shapes.Site(10.1, 10.0), { u'stco': 5.63, u'asset_ref': u'a5629', u'taxonomy': u'rctc-ad-83' }), ((1, 0), shapes.Site(10.0, 10.1), { u'stco': 11.26, u'asset_ref': u'a5630', u'taxonomy': u'rctc-ad-83' }), ((1, 1), shapes.Site(10.1, 10.1), { u'stco': 5.5, u'asset_ref': u'a5636', u'taxonomy': u'rctc-ad-83' }), ] assets = emdl.exposuredata_set.filter(taxonomy="rctc-ad-83"). \ order_by("id") for idx, (gcoo, site, adata) in enumerate(asset_data): if not assets: location = geos.GEOSGeometry(site.point.to_wkt()) asset = models.ExposureData(exposure_model=emdl, site=location, **adata) asset.save() else: asset = assets[idx] GRID_ASSETS[gcoo] = asset self.grid = shapes.Grid( shapes.Region.from_coordinates([(10.0, 10.0), (10.0, 10.1), (10.1, 10.1), (10.1, 10.0)]), 0.1) # this is the expected output of grid_assets_iterator and an input of # asset_losses_per_site self.grid_assets = [ (shapes.GridPoint(self.grid, 0, 0), GRID_ASSETS[(0, 0)]), (shapes.GridPoint(self.grid, 1, 0), GRID_ASSETS[(0, 1)]), (shapes.GridPoint(self.grid, 0, 1), GRID_ASSETS[(1, 0)]), (shapes.GridPoint(self.grid, 1, 1), GRID_ASSETS[(1, 1)]) ]