def test_filter_region_constraint_one_site(self): # look for sites within specified rectangle # constraint is met by one and only one site in the example file # 9.15333 45.12200 region_constraint = shapes.RegionConstraint.from_simple( (9.15332, 45.12201), (9.15334, 45.12199)) ep = exposure.ExposurePortfolioFile( os.path.join(helpers.SCHEMA_EXAMPLES_DIR, TEST_FILE)) expected_result = [ (shapes.Point(9.15333, 45.12200), {'listID': 'PAV01', 'listDescription': 'Collection of existing building in ' \ 'downtown Pavia', 'assetID': 'asset_02', 'assetDescription': 'Moment-resisting non-ductile concrete ' \ 'frame high rise', 'vulnerabilityFunctionReference': 'RC/DMRF-D/LR', 'structureCategory': 'RC-HR-PC', 'assetValue': 250000.0, 'assetValueUnit': 'EUR', })] ctr = None for ctr, (exposure_point, exposure_attr) in enumerate(ep.filter(region_constraint)): # check topological equality for points self.assertTrue( exposure_point.equals(expected_result[ctr][0]), "filter yielded unexpected point at position %s: %s, %s" % (ctr, exposure_point, expected_result[ctr][0])) self.assertTrue(exposure_attr == expected_result[ctr][1], "filter yielded unexpected attribute values at position " \ "%s: %s, %s" % (ctr, exposure_attr, expected_result[ctr][1])) # ensure that generator yielded at least one item self.assertTrue(ctr is not None, "filter yielded nothing although %s item(s) were expected" % \ len(expected_result)) # ensure that generator returns exactly the number of items of the # expected result list self.assertTrue( ctr == len(expected_result) - 1, "filter yielded wrong number of items (%s), expected were %s" % (ctr + 1, len(expected_result)))
def test_filter_region_constraint_one_site(self): # look for sites within specified rectangle # constraint is met by one and only one site in the example file # (lon=16.35/lat=48.25) region_constraint = shapes.RegionConstraint.from_simple((-123.0, 38.0), (-122.0, 37.0)) expected_result = [(shapes.Point(-122.5, 37.5), { 'IMT': 'PGA', 'IDmodel': 'PGA_1_1', 'investigationTimeSpan': 50.0, 'endBranchLabel': '1_1', 'saDamping': 0.2, 'saPeriod': 0.1, 'IMLValues': [5.0000e-03, 7.0000e-03, 1.3700e-02], 'PoEValues': [9.8728e-01, 9.8266e-01, 9.4957e-01] })] counter = None for counter, (nrml_point, nrml_attr) in enumerate( self.nrml_element.filter(region_constraint)): # check topological equality for points self.assertTrue( nrml_point.equals(expected_result[counter][0]), "filter yielded unexpected point at position %s: %s, %s" % (counter, nrml_point, expected_result[counter][0])) self.assertEqual(nrml_attr, expected_result[counter][1], "filter yielded unexpected attribute values at position " \ "%s: %s, %s" % (counter, nrml_attr, expected_result[counter][1])) # ensure that generator yielded at least one item self.assertTrue(counter is not None, "filter yielded nothing although %s item(s) were expected" % \ len(expected_result)) # ensure that generator returns exactly the number of items of the # expected result list self.assertEqual( counter, len(expected_result) - 1, "filter yielded wrong number of items (%s), expected were %s" % (counter + 1, len(expected_result)))
def test_filter_attribute_constraint(self): """ This test uses the attribute constraint filter to select items from the input file. We assume here that the parser yields the items in the same order as specified in the example XML file. In a general case it would be better not to assume the order of yielded items to be known, but to locate each yielded result item in a set of expected results. """ test_filters = [ {'IMT': 'PGA'}, {'IMT': 'FAKE'}, {'IMLValues': [0.0001, 0.0002, 0.0003]}, {'PoEValues': [9.2e-01, 9.15e-01, 9.05e-01]}] # this is the structure of the test NRML file # we'll use this to construct an 'expected results' list nrml_data = [ # first hazardCurveField (shapes.Point(-122.5000, 37.5000), {'IMT': 'PGA', 'IDmodel': 'PGA_1_1', 'investigationTimeSpan': 50.0, 'endBranchLabel': '1_1', 'saDamping': 0.2, 'saPeriod': 0.1, 'IMLValues': [5.0000e-03, 7.0000e-03, 1.3700e-02], 'PoEValues': [9.8728e-01, 9.8266e-01, 9.4957e-01]}), (shapes.Point(-123.5000, 37.5000), {'IMT': 'PGA', 'IDmodel': 'PGA_1_1', 'investigationTimeSpan': 50.0, 'endBranchLabel': '1_1', 'saDamping': 0.2, 'saPeriod': 0.1, 'IMLValues': [5.0000e-03, 7.0000e-03, 1.3700e-02], 'PoEValues': [9.8728e-02, 9.8266e-02, 9.4957e-02]}), # second hazardCurveField (shapes.Point(-125.5000, 37.5000), {'IMT': 'PGA', 'IDmodel': 'PGA_1_1', 'investigationTimeSpan': 50.0, 'endBranchLabel': '1_1', 'saDamping': 0.2, 'saPeriod': 0.1, 'IMLValues': [0.0001, 0.0002, 0.0003], 'PoEValues': [9.3e-01, 9.2e-01, 9.1e-01]}), # third hazardCurveField (shapes.Point(-125.5000, 37.5000), {'IMT': 'PGA', 'IDmodel': 'PGA_1_1', 'investigationTimeSpan': 50.0, 'endBranchLabel': '1_1', 'saDamping': 0.2, 'saPeriod': 0.1, 'IMLValues': [0.0001, 0.0002, 0.0003], 'PoEValues': [9.2e-01, 9.15e-01, 9.05e-01]})] expected_results = [ # one list of results for each test_filter nrml_data, [], [nrml_data[2], nrml_data[3]], [nrml_data[3]]] # set a region constraint that inlcudes all points region_constraint = shapes.RegionConstraint.from_simple( (-126.0, 40.0), (-120.0, 30.0)) for filter_counter, filter_dict in enumerate( test_filters): attribute_constraint = producer.AttributeConstraint( filter_dict) counter = None for counter, (nrml_point, nrml_attr) in enumerate( self.nrml_element.filter(region_constraint, attribute_constraint)): if expected_results[filter_counter]: # only perform the following tests if the expected # result item is not empty expected_nrml_point = \ expected_results[filter_counter][counter][0] expected_nrml_attr = \ expected_results[filter_counter][counter][1] # check topological equality for points self.assertTrue(nrml_point.equals(expected_nrml_point), "filter yielded unexpected point at position" \ " %s: \n Got: %s, \n Expected: %s " \ % (counter, nrml_point, expected_nrml_point)) self.assertEqual(nrml_attr, expected_nrml_attr, "filter yielded unexpected attribute values at " \ "position %s: \n Got: %s, \n Expected: %s " \ % (counter, nrml_attr, expected_nrml_attr)) if expected_results[filter_counter]: # ensure that generator yielded at least one item self.assertTrue(counter is not None, "filter yielded nothing although %s item(s) were expected \ for attribute check of %s" % \ (len(expected_results[filter_counter]), attribute_constraint.attribute)) # ensure that the generator returns _exactly_ the number of # items in the expected result list self.assertEqual(len(expected_results[filter_counter]), counter + 1, "filter yielded incorrect number of items \ \n Got: %s \n Expected: %s" \ % (counter, len(expected_results[filter_counter]))) else: # verify that 0 elements were received self.assertTrue(counter is None) self.nrml_element.reset()