def test_geometry_inputs(self): # Tests the geometry definitions simple_point = Point(2.0, 3.0) simple_point_array = np.array([2.0, 3.0]) # Using nhlib.geo.polygon.Polygon class as input self.point_source.create_geometry(simple_point, 0.0, 30.0) # Check that geometry is an instance of nhlib.geo.polygon.Polygon self.assertTrue(isinstance(self.point_source.geometry, Point)) self.assertAlmostEqual(self.point_source.geometry.longitude, 2.0) self.assertAlmostEqual(self.point_source.geometry.latitude, 3.0) self.assertAlmostEqual(self.point_source.geometry.depth, 0.0) self.assertAlmostEqual(0.0, self.point_source.upper_depth) self.assertAlmostEqual(30.0, self.point_source.lower_depth) self.point_source = mtkPointSource('101', 'A Point Source') # Using numpy array as input self.point_source.create_geometry(simple_point_array, 0.0, 30.0) self.assertTrue(isinstance(self.point_source.geometry, Point)) self.assertAlmostEqual(self.point_source.geometry.longitude, 2.0) self.assertAlmostEqual(self.point_source.geometry.latitude, 3.0) self.assertAlmostEqual(self.point_source.geometry.depth, 0.0) self.assertAlmostEqual(0.0, self.point_source.upper_depth) self.assertAlmostEqual(30.0, self.point_source.lower_depth) # For any other input type - check ValueError is raised self.point_source = mtkPointSource('101', 'A Point Source') with self.assertRaises(ValueError) as ver: self.point_source.create_geometry('a bad input', 0.0, 30.0) self.assertEqual(str(ver.exception), 'Unrecognised or unsupported geometry definition')
def test_geometry_inputs(self): # Tests the geometry definitions simple_point = Point(2.0, 3.0) simple_point_array = np.array([2.0, 3.0]) # Using nhlib.geo.polygon.Polygon class as input self.point_source.create_geometry(simple_point, 0.0, 30.0) # Check that geometry is an instance of nhlib.geo.polygon.Polygon self.assertTrue(isinstance(self.point_source.geometry, Point)) self.assertAlmostEqual(self.point_source.geometry.longitude, 2.0) self.assertAlmostEqual(self.point_source.geometry.latitude, 3.0) self.assertAlmostEqual(self.point_source.geometry.depth, 0.0) self.assertAlmostEqual(0.0, self.point_source.upper_depth) self.assertAlmostEqual(30.0, self.point_source.lower_depth) self.point_source = mtkPointSource('101', 'A Point Source') # Using numpy array as input self.point_source.create_geometry(simple_point_array, 0.0, 30.0) self.assertTrue(isinstance(self.point_source.geometry, Point)) self.assertAlmostEqual(self.point_source.geometry.longitude, 2.0) self.assertAlmostEqual(self.point_source.geometry.latitude, 3.0) self.assertAlmostEqual(self.point_source.geometry.depth, 0.0) self.assertAlmostEqual(0.0, self.point_source.upper_depth) self.assertAlmostEqual(30.0, self.point_source.lower_depth) # For any other input type - check ValueError is raised self.point_source = mtkPointSource('101', 'A Point Source') with self.assertRaises(ValueError) as ver: self.point_source.create_geometry('a bad input', 0.0, 30.0) self.assertEqual(str(ver.exception), 'Unrecognised or unsupported geometry definition')
def parse_point_source_node(node, mfd_spacing=0.1): """ Returns an "areaSource" node into an instance of the :class: openquake.hmtk.sources.area.mtkAreaSource """ assert "pointSource" in node.tag pnt_taglist = get_taglist(node) # Get metadata point_id, name, trt = (node.attrib["id"], node.attrib["name"], node.attrib["tectonicRegion"]) assert point_id # Defensive validation! # Process geometry location, upper_depth, lower_depth = node_to_point_geometry( node.nodes[pnt_taglist.index("pointGeometry")]) # Process scaling relation msr = node_to_scalerel(node.nodes[pnt_taglist.index("magScaleRel")]) # Process aspect ratio aspect = float_(node.nodes[pnt_taglist.index("ruptAspectRatio")].text) # Process MFD mfd = node_to_mfd(node, pnt_taglist) # Process nodal planes npds = node_to_nodal_planes( node.nodes[pnt_taglist.index("nodalPlaneDist")]) # Process hypocentral depths hdds = node_to_hdd(node.nodes[pnt_taglist.index("hypoDepthDist")]) return mtkPointSource(point_id, name, trt, geometry=location, upper_depth=upper_depth, lower_depth=lower_depth, mag_scale_rel=msr, rupt_aspect_ratio=aspect, mfd=mfd, nodal_plane_dist=npds, hypo_depth_dist=hdds)
def test_select_events_within_cell(self): # Tests the selection of events within a cell centred on the point self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.point_source.create_geometry(simple_point, 0., 30.) self.catalogue = Catalogue() self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # Simple case - 200 km by 200 km cell centred on point self.point_source.select_catalogue_within_cell(selector0, 100.) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.point_source.catalogue.data['depth'])
def test_select_events_within_cell(self): # Tests the selection of events within a cell centred on the point self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.point_source.create_geometry(simple_point, 0., 30.) self.catalogue = Catalogue() self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # Simple case - 200 km by 200 km cell centred on point self.point_source.select_catalogue_within_cell(selector0, 100.) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.point_source.catalogue.data['depth'])
def parse_point_source_node(node, mfd_spacing=0.1): """ Returns an "areaSource" node into an instance of the :class: openquake.hmtk.sources.area.mtkAreaSource """ assert "pointSource" in node.tag pnt_taglist = get_taglist(node) # Get metadata point_id, name, trt = (node.attrib["id"], node.attrib["name"], node.attrib["tectonicRegion"]) assert point_id # Defensive validation! # Process geometry location, upper_depth, lower_depth = node_to_point_geometry( node.nodes[pnt_taglist.index("pointGeometry")]) # Process scaling relation msr = node_to_scalerel(node.nodes[pnt_taglist.index("magScaleRel")]) # Process aspect ratio aspect = float_(node.nodes[pnt_taglist.index("ruptAspectRatio")].text) # Process MFD mfd = node_to_mfd(node, pnt_taglist) # Process nodal planes npds = node_to_nodal_planes( node.nodes[pnt_taglist.index("nodalPlaneDist")]) # Process hypocentral depths hdds = node_to_hdd(node.nodes[pnt_taglist.index("hypoDepthDist")]) return mtkPointSource(point_id, name, trt, geometry=location, upper_depth=upper_depth, lower_depth=lower_depth, mag_scale_rel=msr, rupt_aspect_ratio=aspect, mfd=mfd, nodal_plane_dist=npds, hypo_depth_dist=hdds)
def test_select_catalogue(self): # Tests the select_catalogue function - essentially a wrapper to the # two selection functions self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.point_source.create_geometry(simple_point, 0., 30.) # Bad case - no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.point_source.select_catalogue(selector0, 100.) self.assertEqual(str(ver.exception), 'No events found in catalogue!') # Create a catalogue self.catalogue = Catalogue() self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # To ensure that square function is called - compare against direct # instance # First implementation - compare select within distance self.point_source.select_catalogue_within_distance(selector0, 100., 'epicentral') expected_catalogue = deepcopy(self.point_source.catalogue) self.point_source.catalogue = None # Reset catalogue self.point_source.select_catalogue(selector0, 100., 'circle') np.testing.assert_array_equal( self.point_source.catalogue.data['eventID'], expected_catalogue.data['eventID']) # Second implementation - compare select within cell expected_catalogue = None self.point_source.select_catalogue_within_cell(selector0, 150.) expected_catalogue = deepcopy(self.point_source.catalogue) self.point_source.catalogue = None # Reset catalogue self.point_source.select_catalogue(selector0, 150., 'square') np.testing.assert_array_equal( self.point_source.catalogue.data['eventID'], expected_catalogue.data['eventID']) # Finally ensure error is raised when input is neither # 'circle' nor 'square' with self.assertRaises(ValueError) as ver: self.point_source.select_catalogue(selector0, 100., 'bad input') self.assertEqual(str(ver.exception), 'Unrecognised selection type for point source!')
def test_select_catalogue(self): # Tests the select_catalogue function - essentially a wrapper to the # two selection functions self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.point_source.create_geometry(simple_point, 0., 30.) # Bad case - no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.point_source.select_catalogue(selector0, 100.) self.assertEqual(str(ver.exception), 'No events found in catalogue!') # Create a catalogue self.catalogue = Catalogue() self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # To ensure that square function is called - compare against direct # instance # First implementation - compare select within distance self.point_source.select_catalogue_within_distance(selector0, 100., 'epicentral') expected_catalogue = deepcopy(self.point_source.catalogue) self.point_source.catalogue = None # Reset catalogue self.point_source.select_catalogue(selector0, 100., 'circle') np.testing.assert_array_equal( self.point_source.catalogue.data['eventID'], expected_catalogue.data['eventID']) # Second implementation - compare select within cell expected_catalogue = None self.point_source.select_catalogue_within_cell(selector0, 150.) expected_catalogue = deepcopy(self.point_source.catalogue) self.point_source.catalogue = None # Reset catalogue self.point_source.select_catalogue(selector0, 150., 'square') np.testing.assert_array_equal( self.point_source.catalogue.data['eventID'], expected_catalogue.data['eventID']) # Finally ensure error is raised when input is neither # 'circle' nor 'square' with self.assertRaises(ValueError) as ver: self.point_source.select_catalogue(selector0, 100., 'bad input') self.assertEqual(str(ver.exception), 'Unrecognised selection type for point source!')
def test_core_instantiation(self): ''' Simple test to ensure the class is correctly instantiated ''' self.source_model = mtkSourceModel('101', 'Model Name') self.assertEqual(self.source_model.id, '101') self.assertEqual(self.source_model.name, 'Model Name') # No sources on input self.assertEqual(self.source_model.get_number_sources(), 0) # Input correctly good_model = [mtkPointSource('101', 'Point 1'), mtkPointSource('102', 'Point 2')] self.source_model = mtkSourceModel('1001', 'Good Model', good_model) self.assertEqual(self.source_model.get_number_sources(), 2) # Input incorrectly - source not as list with self.assertRaises(ValueError) as ver: self.source_model = mtkSourceModel( '1002', 'Bad Model', mtkPointSource('103', 'Point 3')) self.assertEqual(str(ver.exception), 'Sources must be input as list!')
def test_core_instantiation(self): ''' Simple test to ensure the class is correctly instantiated ''' self.source_model = mtkSourceModel('101', 'Model Name') self.assertEqual(self.source_model.id, '101') self.assertEqual(self.source_model.name, 'Model Name') # No sources on input self.assertEqual(self.source_model.get_number_sources(), 0) # Input correctly good_model = [mtkPointSource('101', 'Point 1'), mtkPointSource('102', 'Point 2')] self.source_model = mtkSourceModel('1001', 'Good Model', good_model) self.assertEqual(self.source_model.get_number_sources(), 2) # Input incorrectly - source not as list with self.assertRaises(ValueError) as ver: self.source_model = mtkSourceModel( '1002', 'Bad Model', mtkPointSource('103', 'Point 3')) self.assertEqual(str(ver.exception), 'Sources must be input as list!')
def test_select_events_in_circular_distance(self): # Basic test of method to select events within a distance of the point self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) # Simple Case - 100 km epicentral distance selector0 = CatalogueSelector(self.catalogue) self.point_source.create_geometry(simple_point, 0., 30.) self.point_source.select_catalogue_within_distance(selector0, 100., 'epicentral') np.testing.assert_array_almost_equal( np.array([0, 1, 2]), self.point_source.catalogue.data['eventID']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.point_source.catalogue.data['depth']) # Simple case - 100 km hypocentral distance (hypocentre at 70 km) self.point_source.select_catalogue_within_distance( selector0, 100., 'hypocentral', 70.) np.testing.assert_array_almost_equal( np.array([1]), self.point_source.catalogue.data['eventID']) np.testing.assert_array_almost_equal( np.array([4.5]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4.5]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1.]), self.point_source.catalogue.data['depth'])
def test_select_events_in_circular_distance(self): # Basic test of method to select events within a distance of the point self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) # Simple Case - 100 km epicentral distance selector0 = CatalogueSelector(self.catalogue) self.point_source.create_geometry(simple_point, 0., 30.) self.point_source.select_catalogue_within_distance(selector0, 100., 'epicentral') np.testing.assert_array_almost_equal( np.array([0, 1, 2]), self.point_source.catalogue.data['eventID']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.point_source.catalogue.data['depth']) # Simple case - 100 km hypocentral distance (hypocentre at 70 km) self.point_source.select_catalogue_within_distance( selector0, 100., 'hypocentral', 70.) np.testing.assert_array_almost_equal( np.array([1]), self.point_source.catalogue.data['eventID']) np.testing.assert_array_almost_equal( np.array([4.5]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4.5]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1.]), self.point_source.catalogue.data['depth'])
def test_create_oq_hazardlib_point_source(self): # Tests the function to create a point source model mfd1 = TruncatedGRMFD(5.0, 8.0, 0.1, 3.0, 1.0) self.point_source = mtkPointSource('001', 'A Point Source', trt='Active Shallow Crust', geometry=Point(10., 10.), upper_depth=0., lower_depth=20., mag_scale_rel=None, rupt_aspect_ratio=1.0, mfd=mfd1, nodal_plane_dist=None, hypo_depth_dist=None) test_source = self.point_source.create_oqhazardlib_source( TOM, 2.0, True) self.assertIsInstance(test_source, PointSource) self.assertIsInstance(test_source.mfd, TruncatedGRMFD) self.assertAlmostEqual(test_source.mfd.b_val, 1.0) self.assertIsInstance(test_source.nodal_plane_distribution, PMF) self.assertIsInstance(test_source.hypocenter_distribution, PMF) self.assertIsInstance(test_source.magnitude_scaling_relationship, WC1994)
def test_create_oq_hazardlib_point_source(self): # Tests the function to create a point source model mfd1 = TruncatedGRMFD(5.0, 8.0, 0.1, 3.0, 1.0) self.point_source = mtkPointSource( '001', 'A Point Source', trt='Active Shallow Crust', geometry=Point(10., 10.), upper_depth=0., lower_depth=20., mag_scale_rel=None, rupt_aspect_ratio=1.0, mfd=mfd1, nodal_plane_dist=None, hypo_depth_dist=None) test_source = self.point_source.create_oqhazardlib_source( TOM, 2.0, True) self.assertIsInstance(test_source, PointSource) self.assertIsInstance(test_source.mfd, TruncatedGRMFD) self.assertAlmostEqual(test_source.mfd.b_val, 1.0) self.assertIsInstance(test_source.nodal_plane_distribution, PMF) self.assertIsInstance(test_source.hypocenter_distribution, PMF) self.assertIsInstance(test_source.magnitude_scaling_relationship, WC1994)
def setUp(self): warnings.simplefilter("ignore") # Suppress warnings during test self.catalogue = Catalogue() self.point_source = mtkPointSource('101', 'A Point Source')
def setUp(self): warnings.simplefilter("ignore") # Suppress warnings during test self.catalogue = Catalogue() self.point_source = mtkPointSource('101', 'A Point Source')