def _test(self, mesh_points, target_points, expected_distance_indexes): mesh = Mesh.from_points_list(mesh_points) target_mesh = Mesh.from_points_list(target_points) dists = mesh.get_min_distance(target_mesh) expected_dists = [mesh_points[mi].distance(target_points[ti]) for ti, mi in enumerate(expected_distance_indexes)] self.assertEqual(list(dists), expected_dists)
def test_mesh_and_point_not_on_surface(self): self._test(Mesh.from_points_list( [Point(0, 0, 1), Point(0, 1, 2), Point(0, 2, 3)]), Mesh.from_points_list( [Point(0, 1.5, 3), Point(0, 1.5, 0.9)]), expected_distance_indices=[2, 1])
def test_nine_positions(self): def v2p(*vectors): # "vectors to points" return [Point(*coords) for coords in zip(*geo_utils.cartesian_to_spherical( numpy.array(vectors, dtype=float) ))] corners = v2p([6370, 0, -0.5], [6370, 0, 0.5], [6369, 2, 0.5], [6369, 2, -0.5]) surface = PlanarSurface(1, 2, 3, *corners) # first three positions: point projection is above the top edge dists = surface.get_min_distance(Mesh.from_points_list( v2p([6371, 0, -1.5], [6371, 0, 1.5], [6371, 0, 0.33]) )) self.assertTrue(numpy.allclose(dists, [2 ** 0.5, 2 ** 0.5, 1.0], atol=1e-4)) # next three positions: point projection is below the bottom edge dists = surface.get_min_distance(Mesh.from_points_list( v2p([6368, 2, -1.5], [6368, 2, 1.5], [6368, 2, -0.45]) )) self.assertTrue(numpy.allclose(dists, [2 ** 0.5, 2 ** 0.5, 1.0], atol=1e-4)) # next three positions: point projection is left to rectangle, # right to it or lies inside dists = surface.get_min_distance(Mesh.from_points_list( v2p([6369.5, 1, -1.5], [6369.5, 1, 1.5], [6369.5, 1, -0.1]) )) self.assertTrue(numpy.allclose(dists, [1, 1, 0], atol=1e-4))
def test_nine_positions(self): def v2p(*vectors): # "vectors to points" return [Point(*coords) for coords in zip(*geo_utils.cartesian_to_spherical( numpy.array(vectors, dtype=float) ))] corners = v2p([6370, 0, -0.5], [6370, 0, 0.5], [6369, 2, 0.5], [6369, 2, -0.5]) surface = PlanarSurface(1, 2, 3, *corners) # first three positions: point projection is above the top edge dists = surface.get_min_distance(Mesh.from_points_list( v2p([6371, 0, -1.5], [6371, 0, 1.5], [6371, 0, 0.33]) )) self.assertTrue(numpy.allclose(dists, [2 ** 0.5, 2 ** 0.5, 1.0], atol=1e-4)) # next three positions: point projection is below the bottom edge dists = surface.get_min_distance(Mesh.from_points_list( v2p([6368, 2, -1.5], [6368, 2, 1.5], [6368, 2, -0.45]) )) self.assertTrue(numpy.allclose(dists, [2 ** 0.5, 2 ** 0.5, 1.0], atol=1e-4)) # next three positions: point projection is left to rectangle, # right to it or lies inside dists = surface.get_min_distance(Mesh.from_points_list( v2p([6369.5, 1, -1.5], [6369.5, 1, 1.5], [6369.5, 1, -0.1]) )) self.assertTrue(numpy.allclose(dists, [1, 1, 0], atol=1e-4))
def test_many_points(self): lons = numpy.array([0.7, 0.6, 0.4, 0.6, 0.3, 0.9, 0.5, 0.4]) lats = numpy.array([0.8, 0.5, 0.2, 0.7, 0.2, 0.4, 0.9, 0.4]) mesh = Mesh(lons, lats, None) polygon = mesh.get_convex_hull() elons = [0.4, 0.3, 0.5, 0.7, 0.9] elats = [0.2, 0.2, 0.9, 0.8, 0.4] numpy.testing.assert_allclose(polygon.lons, elons) numpy.testing.assert_allclose(polygon.lats, elats)
def test_2d_mesh(self): mesh = Mesh(numpy.array([[0., 1.], [2., 3.]]), numpy.array([[0., 0.], [0., 0.]]), None) target_mesh = Mesh( numpy.array([[3., 4., 5.], [-6., -7., 8.], [9., 10., 11.]]), numpy.array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]), None) self._test(mesh, target_mesh, expected_distance_indices=[3, 3, 3, 0, 0, 3, 3, 3, 3])
def test_many_points(self): lons = numpy.array([0.7, 0.6, 0.4, 0.6, 0.3, 0.9, 0.5, 0.4]) lats = numpy.array([0.8, 0.5, 0.2, 0.7, 0.2, 0.4, 0.9, 0.4]) mesh = Mesh(lons, lats, None) polygon = mesh.get_convex_hull() elons = [0.4, 0.3, 0.5, 0.7, 0.9] elats = [0.2, 0.2, 0.9, 0.8, 0.4] numpy.testing.assert_allclose(polygon.lons, elons) numpy.testing.assert_allclose(polygon.lats, elats)
def test_two_points(self): mesh = Mesh(numpy.array([-10., -11.]), numpy.array([-12., -13.]), None) polygon = mesh.get_convex_hull() self.assertIsInstance(polygon, Polygon) elons = [-10.99996704, -11.0000323, -11.00003296, -10.00003295, -9.99996795, -9.99996705] elats = [-13.00003147, -13.00003212, -12.99996853, -11.99996865, -11.99996776, -12.00003135] numpy.testing.assert_allclose(polygon.lons, elons) numpy.testing.assert_allclose(polygon.lats, elats)
def test(self): mesh = Mesh(numpy.array([0., 1., 2., 3.]), numpy.zeros(4), None) matrix = mesh.get_distance_matrix() aaae = numpy.testing.assert_array_almost_equal aaae(matrix[0], [[0, 111.2, 222.4, 333.6]], decimal=1) aaae(matrix[1], [[111.2, 0, 111.2, 222.4]], decimal=1) aaae(matrix[2], [[222.4, 111.2, 0, 111.2]], decimal=1) aaae(matrix[3], [[333.6, 222.4, 111.2, 0]], decimal=1) for i in xrange(4): for j in xrange(i, 4): self.assertEqual(matrix[i, j], matrix[j, i])
def test(self): mesh = Mesh(numpy.array([0., 1., 2., 3.]), numpy.zeros(4), None) matrix = mesh.get_distance_matrix() aaae = numpy.testing.assert_array_almost_equal aaae(matrix[0], [[0, 111.2, 222.4, 333.6]], decimal=1) aaae(matrix[1], [[111.2, 0, 111.2, 222.4]], decimal=1) aaae(matrix[2], [[222.4, 111.2, 0, 111.2]], decimal=1) aaae(matrix[3], [[333.6, 222.4, 111.2, 0]], decimal=1) for i in xrange(4): for j in xrange(i, 4): self.assertEqual(matrix[i, j], matrix[j, i])
def test_two_points(self): mesh = Mesh(numpy.array([-10., -11.]), numpy.array([-12., -13.]), None) polygon = mesh.get_convex_hull() self.assertIsInstance(polygon, Polygon) elons = [ -10.99996704, -11.0000323, -11.00003296, -10.00003295, -9.99996795, -9.99996705 ] elats = [ -13.00003147, -13.00003212, -12.99996853, -11.99996865, -11.99996776, -12.00003135 ] numpy.testing.assert_allclose(polygon.lons, elons) numpy.testing.assert_allclose(polygon.lats, elats)
def test_simple(self): lons = numpy.array([numpy.arange(-1, 1.2, 0.2)] * 11) lats = lons.transpose() + 1 depths = lats + 10 mesh = RectangularMesh(lons, lats, depths) check = lambda lon, lat, depth, expected_distance, **kwargs: \ self.assertAlmostEqual( mesh.get_joyner_boore_distance( Mesh.from_points_list([Point(lon, lat, depth)]) )[0], expected_distance, **kwargs ) check(lon=0, lat=0.5, depth=0, expected_distance=0) check(lon=1, lat=1, depth=0, expected_distance=0) check(lon=0.6, lat=-1, depth=0, expected_distance=Point(0.6, -1).distance(Point(0.6, 0)), delta=0.1) check(lon=-0.8, lat=2.1, depth=10, expected_distance=Point(-0.8, 2.1).distance(Point(-0.8, 2)), delta=0.02) check(lon=0.75, lat=2.3, depth=3, expected_distance=Point(0.75, 2.3).distance(Point(0.75, 2)), delta=0.04)
def test_one_point(self): mesh = Mesh.from_points_list([Point(7, 7)]) polygon = mesh.get_convex_hull() elons = [7.0000453, 7., 6.9999547, 7] elats = [7., 6.99995503, 7., 7.00004497] numpy.testing.assert_allclose(polygon.lons, elons) numpy.testing.assert_allclose(polygon.lats, elats)
def test_point_on_the_border(self): corners = [Point(0.1, -0.1, 1), Point(-0.1, -0.1, 1), Point(-0.1, 0.1, 2), Point(0.1, 0.1, 2)] surface = PlanarSurface(1, 270, 45, *corners) sites = Mesh.from_points_list([Point(-0.1, 0.04), Point(0.1, 0.03)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 2 self.assertTrue(numpy.allclose(dists, expected_dists))
def test_point_outside(self): corners = [Point(0.1, -0.1, 1), Point(-0.1, -0.1, 1), Point(-0.1, 0.1, 2), Point(0.1, 0.1, 2)] surface = PlanarSurface(1, 270, 45, *corners) sites = Mesh.from_points_list( [ Point(-0.2, -0.2), Point(1, 1, 1), Point(4, 5), Point(0.8, 0.01), Point(0.2, -0.15), Point(0.02, -0.12), Point(-0.14, 0), Point(-3, 3), Point(0.05, 0.15, 10), ] ) dists = surface.get_joyner_boore_distance(sites) expected_dists = [ Point(-0.2, -0.2).distance(Point(-0.1, -0.1)), Point(1, 1).distance(Point(0.1, 0.1)), Point(4, 5).distance(Point(0.1, 0.1)), Point(0.8, 0.01).distance(Point(0.1, 0.01)), Point(0.2, -0.15).distance(Point(0.1, -0.1)), Point(0.02, -0.12).distance(Point(0.02, -0.1)), Point(-0.14, 0).distance(Point(-0.1, 0)), Point(-3, 3).distance(Point(-0.1, 0.1)), Point(0.05, 0.15).distance(Point(0.05, 0.1)), ] self.assertTrue(numpy.allclose(dists, expected_dists, atol=0.05))
def test_top_or_bottom_edge_is_closest(self): sites = Mesh.from_points_list([Point(-0.04, -0.28, 0), Point(0.033, 0.15, 0)]) res = self.surface.get_closest_points(sites) aae = numpy.testing.assert_almost_equal aae(res.lons, [-0.04, 0.033], decimal=5) aae(res.lats, [-0.1, 0.1], decimal=5) aae(res.depths, [0, 2], decimal=2)
def test_left_or_right_edge_is_closest(self): sites = Mesh.from_points_list([Point(-0.24, -0.08, 0.55), Point(0.17, 0.07, 0)]) res = self.surface.get_closest_points(sites) aae = numpy.testing.assert_almost_equal aae(res.lons, [-0.1, 0.1], decimal=5) aae(res.lats, [-0.08, 0.07], decimal=3) aae(res.depths, [0.20679306, 1.69185737])
def test_point_inside(self): corners = [[(-1, -1, 1), (1, -1, 1)], [(-1, 1, 2), (1, 1, 2)]] surface = DummySurface(corners) sites = Mesh.from_points_list([Point(0, 0), Point(0, 0, 20), Point(0.1, 0.3)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 3 self.assertTrue(numpy.allclose(dists, expected_dists))
def test_point_on_the_border(self): corners = [[(0.1, -0.1, 1), (-0.1, -0.1, 1)], [(0.1, 0.1, 2), (-0.1, 0.1, 2)]] surface = DummySurface(corners) sites = Mesh.from_points_list([Point(-0.1, 0.04), Point(0.1, 0.03)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 2 self.assertTrue(numpy.allclose(dists, expected_dists, atol=1e-4))
def test4_site_along_strike(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.2, 0), Point(67.6, 0), Point(90.33, 0)]) dists = surface.get_rx_distance(sites) expected_dists = [0] * 3 self.assertTrue(numpy.allclose(dists, expected_dists))
def test_one_point(self): mesh = Mesh.from_points_list([Point(7, 7)]) polygon = mesh.get_convex_hull() elons = [7.0000453, 7., 6.9999547, 7] elats = [7., 6.99995503, 7., 7.00004497] numpy.testing.assert_allclose(polygon.lons, elons) numpy.testing.assert_allclose(polygon.lats, elats)
def test8_strike_of_45_degrees(self): corners = [Point(-0.05, -0.05, 8), Point(0.05, 0.05, 8), Point(0.05, 0.05, 9), Point(-0.05, -0.05, 9)] surface = PlanarSurface(1, 45, 60, *corners) sites = Mesh.from_points_list([Point(0.05, 0)]) self.assertAlmostEqual(surface.get_rx_distance(sites)[0], 3.9313415355436705, places=4)
def test8_strike_of_45_degrees(self): corners = [Point(-0.05, -0.05, 8), Point(0.05, 0.05, 8), Point(0.05, 0.05, 9), Point(-0.05, -0.05, 9)] surface = PlanarSurface(1, 45, 60, *corners) sites = Mesh.from_points_list([Point(0.05, 0)]) self.assertAlmostEqual(surface.get_rx_distance(sites)[0], 3.9313415355436705, places=4)
def test_simple(self): lons = numpy.array([numpy.arange(-1, 1.2, 0.2)] * 11) lats = lons.transpose() + 1 depths = lats + 10 mesh = RectangularMesh(lons, lats, depths) check = lambda lon, lat, depth, expected_distance, **kwargs: \ self.assertAlmostEqual( mesh.get_joyner_boore_distance( Mesh.from_points_list([Point(lon, lat, depth)]) )[0], expected_distance, **kwargs ) check(lon=0, lat=0.5, depth=0, expected_distance=0) check(lon=1, lat=1, depth=0, expected_distance=0) check(lon=0.6, lat=-1, depth=0, expected_distance=Point(0.6, -1).distance(Point(0.6, 0)), delta=0.1) check(lon=-0.8, lat=2.1, depth=10, expected_distance=Point(-0.8, 2.1).distance(Point(-0.8, 2)), delta=0.02) check(lon=0.75, lat=2.3, depth=3, expected_distance=Point(0.75, 2.3).distance(Point(0.75, 2)), delta=0.04)
def __init__(self, sites): self.indices = None self.vs30 = numpy.zeros(len(sites)) self.vs30measured = numpy.zeros(len(sites), dtype=bool) self.z1pt0 = self.vs30.copy() self.z2pt5 = self.vs30.copy() lons = self.vs30.copy() lats = self.vs30.copy() for i in xrange(len(sites)): self.vs30[i] = sites[i].vs30 self.vs30measured[i] = sites[i].vs30measured self.z1pt0[i] = sites[i].z1pt0 self.z2pt5[i] = sites[i].z2pt5 lons[i] = sites[i].location.longitude lats[i] = sites[i].location.latitude self.mesh = Mesh(lons, lats, depths=None) # protect arrays from being accidentally changed. it is useful # because we pass these arrays directly to a GMPE through # a SiteContext object and if a GMPE is implemented poorly it could # modify the site values, thereby corrupting site and all the # subsequent calculation. note that this doesn't protect arrays from # being changed by calling itemset() for arr in (self.vs30, self.vs30measured, self.z1pt0, self.z2pt5, self.mesh.lons, self.mesh.lats): arr.flags.writeable = False
def test5_site_opposite_to_strike_direction(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(-0.2, 0), Point(-67.6, 0), Point(-90.33, 0)]) dists = surface.get_rx_distance(sites) expected_dists = [0] * 3 self.assertTrue(numpy.allclose(dists, expected_dists))
def test4_site_along_strike(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.2, 0), Point(67.6, 0), Point(90.33, 0)]) dists = surface.get_rx_distance(sites) expected_dists = [0] * 3 self.assertTrue(numpy.allclose(dists, expected_dists))
def test5_site_opposite_to_strike_direction(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(-0.2, 0), Point(-67.6, 0), Point(-90.33, 0)]) dists = surface.get_rx_distance(sites) expected_dists = [0] * 3 self.assertTrue(numpy.allclose(dists, expected_dists))
def test_from_points_list_no_depth(self): points = [Point(0, 1), Point(2, 3), Point(5, 7)] mesh = Mesh.from_points_list(points) self.assertTrue((mesh.lons == [0, 2, 5]).all()) self.assertTrue((mesh.lats == [1, 3, 7]).all()) self.assertEqual(mesh.lons.dtype, numpy.float) self.assertEqual(mesh.lats.dtype, numpy.float) self.assertIs(mesh.depths, None)
def test_point_above_surface(self): sites = Mesh.from_points_list([Point(0, 0), Point(-0.03, 0.05, 0.5)]) res = self.surface.get_closest_points(sites) self.assertIsInstance(res, Mesh) aae = numpy.testing.assert_almost_equal aae(res.lons, [0, -0.03], decimal=4) aae(res.lats, [-0.00081824, 0.04919223]) aae(res.depths, [1.0113781, 1.50822185])
def test_from_points_list_no_depth(self): points = [Point(0, 1), Point(2, 3), Point(5, 7)] mesh = Mesh.from_points_list(points) self.assertTrue((mesh.lons == [0, 2, 5]).all()) self.assertTrue((mesh.lats == [1, 3, 7]).all()) self.assertEqual(mesh.lons.dtype, numpy.float) self.assertEqual(mesh.lats.dtype, numpy.float) self.assertIs(mesh.depths, None)
def test_left_or_right_edge_is_closest(self): sites = Mesh.from_points_list([Point(-0.24, -0.08, 0.55), Point(0.17, 0.07, 0)]) res = self.surface.get_closest_points(sites) aae = numpy.testing.assert_almost_equal aae(res.lons, [-0.1, 0.1], decimal=5) aae(res.lats, [-0.08, 0.07], decimal=3) aae(res.depths, [0.20679306, 1.69185737])
def test_top_or_bottom_edge_is_closest(self): sites = Mesh.from_points_list([Point(-0.04, -0.28, 0), Point(0.033, 0.15, 0)]) res = self.surface.get_closest_points(sites) aae = numpy.testing.assert_almost_equal aae(res.lons, [-0.04, 0.033], decimal=5) aae(res.lats, [-0.1, 0.1], decimal=5) aae(res.depths, [0, 2], decimal=2)
def test_point_above_surface(self): sites = Mesh.from_points_list([Point(0, 0), Point(-0.03, 0.05, 0.5)]) res = self.surface.get_closest_points(sites) self.assertIsInstance(res, Mesh) aae = numpy.testing.assert_almost_equal aae(res.lons, [0, -0.03], decimal=4) aae(res.lats, [-0.00081824, 0.04919223]) aae(res.depths, [1.0113781, 1.50822185])
def test_point_on_the_border(self): corners = [[(0.1, -0.1, 1), (-0.1, -0.1, 1)], [(0.1, 0.1, 2), (-0.1, 0.1, 2)]] surface = DummySurface(corners) sites = Mesh.from_points_list([Point(-0.1, 0.04), Point(0.1, 0.03)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 2 self.assertTrue(numpy.allclose(dists, expected_dists, atol=1e-4))
def test_point_on_the_border(self): corners = [Point(0.1, -0.1, 1), Point(-0.1, -0.1, 1), Point(-0.1, 0.1, 2), Point(0.1, 0.1, 2)] surface = PlanarSurface(1, 270, 45, *corners) sites = Mesh.from_points_list([Point(-0.1, 0.04), Point(0.1, 0.03)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 2 self.assertTrue(numpy.allclose(dists, expected_dists))
def test_mesh_of_one_point(self): lons = numpy.array([[1.]]) lats = numpy.array([[0.]]) depths = numpy.array([[1.]]) mesh = RectangularMesh(lons, lats, depths) target_mesh = Mesh.from_points_list([Point(1, 0), Point(0.5, 0)]) dists = mesh.get_joyner_boore_distance(target_mesh) expected_dists = [0, Point(0.5, 0).distance(Point(1, 0))] self.assertTrue(numpy.allclose(dists, expected_dists, atol=0.2))
def _test(self, points, site, expected_distance): lons, lats, depths = numpy.array(points).transpose() lons = lons.transpose() lats = lats.transpose() depths = depths.transpose() mesh = RectangularMesh(lons, lats, depths) distance = mesh.get_joyner_boore_distance( Mesh.from_points_list([Point(*site)]))[0] self.assertAlmostEqual(distance, expected_distance, delta=0.02)
def test_point_inside(self): corners = [Point(-1, -1, 1), Point(1, -1, 1), Point(1, 1, 2), Point(-1, 1, 2)] surface = PlanarSurface(10, 90, 45, *corners) sites = Mesh.from_points_list([Point(0, 0), Point(0, 0, 20), Point(0.1, 0.3)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 3 self.assertTrue(numpy.allclose(dists, expected_dists))
def test_mesh_of_one_point(self): lons = numpy.array([[1]]) lats = numpy.array([[0]]) depths = numpy.array([[1]]) mesh = RectangularMesh(lons, lats, depths) target_mesh = Mesh.from_points_list([Point(1, 0), Point(0.5, 0)]) dists = mesh.get_joyner_boore_distance(target_mesh) expected_dists = [0, Point(0.5, 0).distance(Point(1, 0))] self.assertTrue(numpy.allclose(dists, expected_dists, atol=0.2))
def test_mesh_of_two_points(self): lons = numpy.array([[0, 0.5, 1]], float) lats = numpy.array([[0, 0, 0]], float) depths = numpy.array([[1, 0, 1]], float) mesh = RectangularMesh(lons, lats, depths) target_mesh = Mesh.from_points_list([Point(0.5, 1), Point(0.5, 0)]) dists = mesh.get_joyner_boore_distance(target_mesh) expected_dists = [Point(0.5, 1).distance(Point(0.5, 0)), 0] numpy.testing.assert_almost_equal(dists, expected_dists)
def test_mesh_of_two_points(self): lons = numpy.array([[0, 0.5, 1]], float) lats = numpy.array([[0, 0, 0]], float) depths = numpy.array([[1, 0, 1]], float) mesh = RectangularMesh(lons, lats, depths) target_mesh = Mesh.from_points_list([Point(0.5, 1), Point(0.5, 0)]) dists = mesh.get_joyner_boore_distance(target_mesh) expected_dists = [Point(0.5, 1).distance(Point(0.5, 0)), 0] numpy.testing.assert_almost_equal(dists, expected_dists)
def test_corner_is_closest(self): sites = Mesh.from_points_list( [Point(-0.11, 0.11), Point(0.14, -0.12, 10), Point(0.3, 0.2, 0.5), Point(-0.6, -0.6, 0.3)] ) res = self.surface.get_closest_points(sites) aae = numpy.testing.assert_almost_equal aae(res.lons, [-0.1, 0.1, 0.1, -0.1], decimal=4) aae(res.lats, [0.1, -0.1, 0.1, -0.1]) aae(res.depths, [2, 0, 2, 0], decimal=5)
def test_corner_is_closest(self): sites = Mesh.from_points_list( [Point(-0.11, 0.11), Point(0.14, -0.12, 10), Point(0.3, 0.2, 0.5), Point(-0.6, -0.6, 0.3)] ) res = self.surface.get_closest_points(sites) aae = numpy.testing.assert_almost_equal aae(res.lons, [-0.1, 0.1, 0.1, -0.1], decimal=4) aae(res.lats, [0.1, -0.1, 0.1, -0.1]) aae(res.depths, [2, 0, 2, 0], decimal=5)
def _test(self, points, site, expected_distance): lons, lats, depths = numpy.array(points).transpose() lons = lons.transpose() lats = lats.transpose() depths = depths.transpose() mesh = RectangularMesh(lons, lats, depths) distance = mesh.get_joyner_boore_distance( Mesh.from_points_list([Point(*site)]) )[0] self.assertAlmostEqual(distance, expected_distance, delta=0.02)
def test_point_inside(self): corners = [[(-0.1, -0.1, 1), (0.1, -0.1, 1)], [(-0.1, 0.1, 2), (0.1, 0.1, 2)]] surface = DummySurface(corners) sites = Mesh.from_points_list( [Point(0, 0), Point(0, 0, 20), Point(0.01, 0.03)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 3 self.assertTrue(numpy.allclose(dists, expected_dists))
def filter(self, mask): """ Create a new collection with only a subset of sites from this one. :param mask: Numpy array of boolean values of the same length as this sites collection. ``True`` values should indicate that site with that index should be included into the filtered collection. :returns: A new :class:`SiteCollection` instance, unless all the values in ``mask`` are ``True``, in which case this site collection is returned, or if all the values in ``mask`` are ``False``, in which case method returns ``None``. New collection has data of only those sites that were marked for inclusion in mask. See also :meth:`expand`. """ assert len(mask) == len(self) if mask.all(): # all sites satisfy the filter, return # this collection unchanged return self if not mask.any(): # no sites pass the filter, return None return None col = object.__new__(SiteCollection) # extract indices of Trues from the mask [indices] = mask.nonzero() # take only needed values from this collection # to a new one col.vs30 = self.vs30.take(indices) col.vs30measured = self.vs30measured.take(indices) col.z1pt0 = self.z1pt0.take(indices) col.z2pt5 = self.z2pt5.take(indices) col.mesh = Mesh(self.mesh.lons.take(indices), self.mesh.lats.take(indices), depths=None) if self.indices is not None: # if this collection was already a subset of some other # collection (a result of :meth:`filter` itself) than mask's # indices represent values in a filtered collection, but # we need to keep track of original indices in the whole # (unfiltered) collection. here we save original indices # of sites in this double- (or more times) filtered # collection col.indices = self.indices.take(indices) else: col.indices = indices # do the same as in the constructor for arr in (col.vs30, col.vs30measured, col.z1pt0, col.z2pt5, col.mesh.lons, col.mesh.lats): arr.flags.writeable = False return col
def test_vertical_mesh(self): lons = numpy.array([[0, 1, 2], [0, 1, 2]]) lats = numpy.array([[0, 0, 0], [0, 0, 0]]) depths = numpy.array([[1, 1, 1], [2, 2, 2]]) mesh = RectangularMesh(lons, lats, depths) target_mesh = Mesh.from_points_list([Point(0.5, 0), Point(0.5, 1), Point(0.5, 5)]) dists = mesh.get_joyner_boore_distance(target_mesh) expected_dists = [ 0, Point(0.5, 1).distance(Point(0.5, 0)), Point(0.5, 5).distance(Point(0.5, 0)) ] self.assertTrue(numpy.allclose(dists, expected_dists, atol=3))
def get_closest_points(self, mesh): """ See :meth:`superclass' method <nhlib.geo.surface.base.BaseSurface.get_closest_points>`. This is an optimized version specific to planar surface that doesn't make use of the mesh. """ dists, xx, yy = self._project(mesh.lons, mesh.lats, mesh.depths) mxx = xx.clip(0, self.length) myy = yy.clip(0, self.width) dists.fill(0) lons, lats, depths = self._project_back(dists, mxx, myy) return Mesh(lons, lats, depths)
def test_point_outside(self): corners = [[(0.1, -0.1, 1), (-0.1, -0.1, 1)], [(0.1, 0.1, 2), (-0.1, 0.1, 2)]] surface = DummySurface(corners) sites = Mesh.from_points_list( [Point(-0.2, -0.2), Point(1, 1, 1), Point(4, 5), Point(8, 10.4), Point(0.05, 0.15, 10)] ) dists = surface.get_joyner_boore_distance(sites) expected_dists = [ Point(-0.2, -0.2).distance(Point(-0.1, -0.1)), Point(1, 1).distance(Point(0.1, 0.1)), Point(4, 5).distance(Point(0.1, 0.1)), Point(8, 10.4).distance(Point(0.1, 0.1)), Point(0.05, 0.15).distance(Point(0.05, 0.1)), ] self.assertTrue(numpy.allclose(dists, expected_dists, atol=0.2))
def surface_projection_from_fault_data(cls, edges): """ Get a surface projection of the complex fault surface. :param edges: A list of horizontal edges of the surface as instances of :class:`nhlib.geo.line.Line`. :returns: Instance of :class:`~nhlib.geo.polygon.Polygon` describing the surface projection of the complex fault. """ # collect lons and lats of all the vertices of all the edges lons, lats = numpy.array([[[point.longitude, point.latitude] for point in edge] for edge in edges], dtype=float).reshape((-1, 2)).transpose() return Mesh(lons, lats, depths=None).get_convex_hull()
def test_against_mesh_to_mesh(self): corners = [Point(2.6, 3.7, 20), Point(2.90102155, 3.99961567, 20), Point(3.2, 3.7, 75), Point(2.89905849, 3.40038407, 75)] surface = PlanarSurface(0.5, 45, 70, *corners) lons, lats = numpy.meshgrid(numpy.linspace(2.2, 3.6, 7), numpy.linspace(3.4, 4.2, 7)) sites = Mesh(lons, lats, depths=None) res1 = surface.get_closest_points(sites) res2 = super(PlanarSurface, surface).get_closest_points(sites) aae = numpy.testing.assert_almost_equal # precision up to ~1 km aae(res1.lons, res2.lons, decimal=2) aae(res1.lats, res2.lats, decimal=2) aae(res1.depths, res2.depths, decimal=0)
def discretize(self, mesh_spacing): """ Get a mesh of uniformly spaced points inside the polygon area with distance of ``mesh_spacing`` km between. :returns: An instance of :class:`~nhlib.geo.mesh.Mesh` that holds the points data. Mesh is created with no depth information (all the points are on the Earth surface). """ self._init_polygon2d() west, east, north, south = self._bbox lons = [] lats = [] # we cover the bounding box (in spherical coordinates) from highest # to lowest latitude and from left to right by longitude. we step # by mesh spacing distance (linear measure). we check each point # if it is inside the polygon and yield the point object, if so. # this way we produce an uniformly-spaced mesh regardless of the # latitude. latitude = north while latitude > south: longitude = west while utils.get_longitudinal_extent(longitude, east) > 0: # we use Cartesian space just for checking if a point # is inside of the polygon. x, y = self._projection(longitude, latitude) if self._polygon2d.contains(shapely.geometry.Point(x, y)): lons.append(longitude) lats.append(latitude) # move by mesh spacing along parallel... longitude, _, = geodetic.point_at(longitude, latitude, 90, mesh_spacing) # ... and by the same distance along meridian in outer one _, latitude = geodetic.point_at(west, latitude, 180, mesh_spacing) lons = numpy.array(lons) lats = numpy.array(lats) return Mesh(lons, lats, depths=None)
def test_point_outside(self): corners = [[(0.1, -0.1, 1), (-0.1, -0.1, 1)], [(0.1, 0.1, 2), (-0.1, 0.1, 2)]] surface = DummySurface(corners) sites = Mesh.from_points_list([ Point(-0.2, -0.2), Point(1, 1, 1), Point(4, 5), Point(8, 10.4), Point(0.05, 0.15, 10) ]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [ Point(-0.2, -0.2).distance(Point(-0.1, -0.1)), Point(1, 1).distance(Point(0.1, 0.1)), Point(4, 5).distance(Point(0.1, 0.1)), Point(8, 10.4).distance(Point(0.1, 0.1)), Point(0.05, 0.15).distance(Point(0.05, 0.1)) ] self.assertTrue(numpy.allclose(dists, expected_dists, atol=0.2))
def test_from_points_list_with_depth(self): points = [Point(0, 1, 2), Point(2, 3, 4), Point(5, 7, 10)] mesh = Mesh.from_points_list(points) self.assertTrue((mesh.depths == [2, 4, 10]).all()) self.assertEqual(mesh.depths.dtype, numpy.float)
def test_4(self): surface = PlanarSurface(1, 2, 3, *test_data.TEST_7_RUPTURE_2_CORNERS) sites = Mesh.from_points_list([Point(-0.3, 0.4)]) self.assertAlmostEqual(55.6159556, surface.get_min_distance(sites)[0], delta=0.6)
def test2_site_on_the_hanging_wall(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.05, -0.05), Point(-140, -0.05)]) dists = surface.get_rx_distance(sites) expected_dists = [5.559752615413244] * 2 self.assertTrue(numpy.allclose(dists, expected_dists))
def test3_site_on_centroid(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.05, 0)]) self.assertAlmostEqual(surface.get_rx_distance(sites)[0], 0)
def test6_one_degree_distance(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.05, -1), Point(20, 1)]) dists = surface.get_rx_distance(sites) expected_dists = [111.19505230826488, -111.19505230826488] self.assertTrue(numpy.allclose(dists, expected_dists))
def test7_ten_degrees_distance(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0, -10), Point(-15, 10)]) dists = surface.get_rx_distance(sites) expected_dists = [1111.9505230826488, -1111.9505230826488] self.assertTrue(numpy.allclose(dists, expected_dists))