From a90cf55e6f9e73302ec60559b567ffaa24732c37 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Tue, 13 Apr 2021 13:21:14 -0300 Subject: [PATCH] Rename prisms_layer for prism_layer Also rename the xarray accessor to DatasetAccessorPrismLayer --- doc/api/index.rst | 4 +- .../{prisms_layer.py => prism_layer.py} | 6 +- examples/forward/prisms_topo_gravity.py | 6 +- harmonica/__init__.py | 2 +- .../{prisms_layer.py => prism_layer.py} | 20 ++-- ...st_prisms_layer.py => test_prism_layer.py} | 102 +++++++++--------- 6 files changed, 70 insertions(+), 70 deletions(-) rename examples/forward/{prisms_layer.py => prism_layer.py} (92%) rename harmonica/forward/{prisms_layer.py => prism_layer.py} (97%) rename harmonica/tests/{test_prisms_layer.py => test_prism_layer.py} (80%) diff --git a/doc/api/index.rst b/doc/api/index.rst index 07007f46b..e7d859a63 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -35,8 +35,8 @@ Forward modelling point_mass_gravity prism_gravity tesseroid_gravity - prisms_layer - DatasetAccessorPrismsLayer + prism_layer + DatasetAccessorPrismLayer Isostasy -------- diff --git a/examples/forward/prisms_layer.py b/examples/forward/prism_layer.py similarity index 92% rename from examples/forward/prisms_layer.py rename to examples/forward/prism_layer.py index 1711a5057..0f2cbfdbe 100644 --- a/examples/forward/prisms_layer.py +++ b/examples/forward/prism_layer.py @@ -10,7 +10,7 @@ One way to model three dimensional structures is to create a set of prisms that approximates their geometry and its physical properties (density, -susceptibility, etc.). The :func:`harmonica.prisms_layer` offers a simple way +susceptibility, etc.). The :func:`harmonica.prism_layer` offers a simple way to create a layer of prisms: a regular grid of prisms of equal size on the horizontal directions with variable top and bottom boundaries. It returns a :class:`xarray.Dataset` with the coordinates of the centers of the prisms and @@ -33,7 +33,7 @@ (easting, northing) = vd.grid_coordinates(region=region, spacing=spacing) surface = 100 * np.exp(-((easting - 50e3) ** 2 + northing ** 2) / 1e9) density = 2670.0 * np.ones_like(surface) -prisms = hm.prisms_layer( +prisms = hm.prism_layer( coordinates=(easting[0, :], northing[:, 0]), surface=surface, reference=0, @@ -42,7 +42,7 @@ # Compute gravity field of prisms on a regular grid of observation points coordinates = vd.grid_coordinates(region, spacing=spacing, extra_coords=1e3) -gravity = prisms.prisms_layer.gravity(coordinates, field="g_z") +gravity = prisms.prism_layer.gravity(coordinates, field="g_z") # Plot gravity field plt.pcolormesh(*coordinates[:2], gravity) diff --git a/examples/forward/prisms_topo_gravity.py b/examples/forward/prisms_topo_gravity.py index dbe28ddfb..be2591498 100644 --- a/examples/forward/prisms_topo_gravity.py +++ b/examples/forward/prisms_topo_gravity.py @@ -8,7 +8,7 @@ Gravitational effect of topography ================================== -One possible application of the :func:`harmonica.prisms_layer` function is to +One possible application of the :func:`harmonica.prism_layer` function is to create a model of the terrain and compute its gravity effect. Here we will use a regular grid of topographic and bathymetric heights for South Africa to create a prisms layer that model the terrain with a density of 2670 kg/m^3 and @@ -43,7 +43,7 @@ density = density.where(south_africa_topo >= 0, 1000 - 2900) # Create layer of prisms -prisms = hm.prisms_layer( +prisms = hm.prism_layer( (south_africa_topo.easting, south_africa_topo.northing), surface=south_africa_topo.values, reference=0, @@ -56,7 +56,7 @@ ) easting, northing = projection(*coordinates[:2]) coordinates_projected = (easting, northing, coordinates[-1]) -prisms_gravity = prisms.prisms_layer.gravity(coordinates_projected, field="g_z") +prisms_gravity = prisms.prism_layer.gravity(coordinates_projected, field="g_z") # Make a plot of the computed gravity plt.figure(figsize=(8, 8)) diff --git a/harmonica/__init__.py b/harmonica/__init__.py index c8ae10f44..15738583b 100644 --- a/harmonica/__init__.py +++ b/harmonica/__init__.py @@ -15,7 +15,7 @@ from .forward.point_mass import point_mass_gravity from .forward.tesseroid import tesseroid_gravity from .forward.prism import prism_gravity -from .forward.prisms_layer import prisms_layer, DatasetAccessorPrismsLayer +from .forward.prism_layer import prism_layer, DatasetAccessorPrismLayer from .equivalent_layer.harmonic import EQLHarmonic from .equivalent_layer.harmonic_spherical import EQLHarmonicSpherical diff --git a/harmonica/forward/prisms_layer.py b/harmonica/forward/prism_layer.py similarity index 97% rename from harmonica/forward/prisms_layer.py rename to harmonica/forward/prism_layer.py index 66140e358..c1e4bf268 100644 --- a/harmonica/forward/prisms_layer.py +++ b/harmonica/forward/prism_layer.py @@ -15,7 +15,7 @@ from .prism import prism_gravity -def prisms_layer( +def prism_layer( coordinates, surface, reference, @@ -32,7 +32,7 @@ def prisms_layer( ``northing`` coordinates correspond to the location of the center of each prism. - The ``prisms_layer`` dataset accessor can be used to access special methods + The ``prism_layer`` dataset accessor can be used to access special methods and attributes for the layer of prisms, like the horizontal dimensions of the prisms, getting the boundaries of each prisms, etc. See :class:`XarrayAcessorPrismLayer` for the definition of these methods @@ -85,7 +85,7 @@ def prisms_layer( >>> surface = np.arange(20, dtype=float).reshape((4, 5)) >>> density = 2670.0 * np.ones_like(surface) >>> # Define a layer of prisms - >>> prisms = prisms_layer( + >>> prisms = prism_layer( ... (easting, northing), ... surface, ... reference=0, @@ -105,10 +105,10 @@ def prisms_layer( coords_units: meters properties_units: SI >>> # Get the boundaries of the layer (will exceed the region) - >>> print(prisms.prisms_layer.boundaries) + >>> print(prisms.prism_layer.boundaries) (-1.25, 11.25, 1.0, 9.0) >>> # Get the boundaries of one of the prisms - >>> print(prisms.prisms_layer.get_prism((0, 2))) + >>> print(prisms.prism_layer.get_prism((0, 2))) (3.75, 6.25, 1.0, 3.0, 0.0, 2.0) """ # noqa: W505 dims = ("northing", "easting") @@ -128,7 +128,7 @@ def prisms_layer( attrs = {"coords_units": "meters", "properties_units": "SI"} prisms.attrs = attrs # Create the top and bottom coordinates of the prisms - prisms.prisms_layer.update_top_bottom(surface, reference) + prisms.prism_layer.update_top_bottom(surface, reference) return prisms @@ -146,20 +146,20 @@ def _check_regular_grid(easting, northing): raise ValueError("Passed northing coordiantes are not evenly spaced.") -@xr.register_dataset_accessor("prisms_layer") -class DatasetAccessorPrismsLayer: +@xr.register_dataset_accessor("prism_layer") +class DatasetAccessorPrismLayer: """ Defines dataset accessor for layer of prisms .. warning:: This class is not intended to be initialized. - Use the `prisms_layer` accessor for accessing the methods and + Use the `prism_layer` accessor for accessing the methods and attributes of this class. See also -------- - harmonica.prisms_layer + harmonica.prism_layer """ def __init__(self, xarray_obj): diff --git a/harmonica/tests/test_prisms_layer.py b/harmonica/tests/test_prism_layer.py similarity index 80% rename from harmonica/tests/test_prisms_layer.py rename to harmonica/tests/test_prism_layer.py index da31a7135..05965e535 100644 --- a/harmonica/tests/test_prisms_layer.py +++ b/harmonica/tests/test_prism_layer.py @@ -14,10 +14,10 @@ import numpy.testing as npt import verde as vd -from .. import prisms_layer, prism_gravity +from .. import prism_layer, prism_gravity -def test_prisms_layer(): +def test_prism_layer(): """ Check if a layer of prisms is property constructed """ @@ -25,7 +25,7 @@ def test_prisms_layer(): northing = np.linspace(7, 13, 4) reference = 0 surface = np.arange(20, dtype=float).reshape(4, 5) - layer = prisms_layer((easting, northing), surface, reference) + layer = prism_layer((easting, northing), surface, reference) assert "easting" in layer.coords assert "northing" in layer.coords assert "top" in layer.coords @@ -39,7 +39,7 @@ def test_prisms_layer(): expected_top = surface.copy() expected_bottom = reference * np.ones_like(surface) expected_top[1, 1], expected_bottom[1, 1] = reference, surface[1, 1] - layer = prisms_layer((easting, northing), surface, reference) + layer = prism_layer((easting, northing), surface, reference) assert "easting" in layer.coords assert "northing" in layer.coords assert "top" in layer.coords @@ -50,7 +50,7 @@ def test_prisms_layer(): npt.assert_allclose(layer.bottom, expected_bottom) -def test_prisms_layer_invalid_surface_reference(): +def test_prism_layer_invalid_surface_reference(): """ Check if invalid surface and/or reference are caught """ @@ -59,15 +59,15 @@ def test_prisms_layer_invalid_surface_reference(): reference = 0 surface = np.arange(20, dtype=float) with pytest.raises(ValueError): - prisms_layer(coordinates, surface, reference) + prism_layer(coordinates, surface, reference) # Reference with wrong shape reference = np.zeros(20) surface = np.arange(20, dtype=float).reshape(4, 5) with pytest.raises(ValueError): - prisms_layer(coordinates, surface, reference) + prism_layer(coordinates, surface, reference) -def test_prisms_layer_properties(): +def test_prism_layer_properties(): """ Check passing physical properties to the prisms layer """ @@ -77,7 +77,7 @@ def test_prisms_layer_properties(): surface = np.arange(20, dtype=float).reshape(4, 5) density = 2670 * np.ones_like(surface) suceptibility = np.arange(20, dtype=float).reshape(4, 5) - layer = prisms_layer( + layer = prism_layer( (easting, northing), surface, reference, @@ -87,7 +87,7 @@ def test_prisms_layer_properties(): npt.assert_allclose(layer.suceptibility, suceptibility) -def test_prisms_layer_no_regular_grid(): +def test_prism_layer_no_regular_grid(): """ Check if error is raised if easting and northing are not regular """ @@ -98,7 +98,7 @@ def test_prisms_layer_no_regular_grid(): northing = np.linspace(7, 13, 4) easting[3] = -22 with pytest.raises(ValueError): - prisms_layer( + prism_layer( (easting, northing), surface, reference, @@ -108,14 +108,14 @@ def test_prisms_layer_no_regular_grid(): northing = np.linspace(7, 13, 4) northing[3] = 12.98 with pytest.raises(ValueError): - prisms_layer( + prism_layer( (easting, northing), surface, reference, ) -def test_prisms_layer_attributes(): +def test_prism_layer_attributes(): """ Check attributes of the DatasetAccessorPrismsLayer class """ @@ -123,44 +123,44 @@ def test_prisms_layer_attributes(): northing = np.linspace(7, 10, 4) reference = 0 surface = np.arange(20, dtype=float).reshape(4, 5) - layer = prisms_layer((easting, northing), surface, reference) - assert layer.prisms_layer.dims == ("northing", "easting") - assert layer.prisms_layer.spacing == (1, 0.5) - assert layer.prisms_layer.boundaries == ( + layer = prism_layer((easting, northing), surface, reference) + assert layer.prism_layer.dims == ("northing", "easting") + assert layer.prism_layer.spacing == (1, 0.5) + assert layer.prism_layer.boundaries == ( easting[0] - 0.25, easting[-1] + 0.25, northing[0] - 0.5, northing[-1] + 0.5, ) - assert layer.prisms_layer.size == 20 - assert layer.prisms_layer.shape == (4, 5) + assert layer.prism_layer.size == 20 + assert layer.prism_layer.shape == (4, 5) -def test_prisms_layer_to_prisms(): +def test_prism_layer_to_prisms(): """ Check the _to_prisms() method """ coordinates = (np.array([0, 1]), np.array([0, 1])) reference = np.arange(4).reshape(2, 2) surface = (np.arange(4) + 10).reshape(2, 2) - layer = prisms_layer(coordinates, surface, reference) + layer = prism_layer(coordinates, surface, reference) expected_prisms = [ [-0.5, 0.5, -0.5, 0.5, 0, 10], [0.5, 1.5, -0.5, 0.5, 1, 11], [-0.5, 0.5, 0.5, 1.5, 2, 12], [0.5, 1.5, 0.5, 1.5, 3, 13], ] - npt.assert_allclose(expected_prisms, layer.prisms_layer._to_prisms()) + npt.assert_allclose(expected_prisms, layer.prism_layer._to_prisms()) -def test_prisms_layer_get_prism_by_index(): +def test_prism_layer_get_prism_by_index(): """ Check if the right prism is returned after index """ coordinates = (np.array([0, 1]), np.array([0, 1])) reference = np.arange(4).reshape(2, 2) surface = (np.arange(4) + 10).reshape(2, 2) - layer = prisms_layer(coordinates, surface, reference) + layer = prism_layer(coordinates, surface, reference) expected_prisms = [ [[-0.5, 0.5, -0.5, 0.5, 0, 10], [0.5, 1.5, -0.5, 0.5, 1, 11]], [[-0.5, 0.5, 0.5, 1.5, 2, 12], [0.5, 1.5, 0.5, 1.5, 3, 13]], @@ -168,7 +168,7 @@ def test_prisms_layer_get_prism_by_index(): for i in range(2): for j in range(2): npt.assert_allclose( - layer.prisms_layer.get_prism((i, j)), expected_prisms[i][j] + layer.prism_layer.get_prism((i, j)), expected_prisms[i][j] ) @@ -184,34 +184,34 @@ def test_nonans_prisms_mask(): # No nan in top nor bottom # ------------------------ - layer = prisms_layer((easting, northing), surface, reference) + layer = prism_layer((easting, northing), surface, reference) expected_mask = np.ones(shape, dtype=bool) - mask = layer.prisms_layer._get_nonans_mask() + mask = layer.prism_layer._get_nonans_mask() npt.assert_allclose(mask, expected_mask) # Nans in top only # ---------------- - layer = prisms_layer((easting, northing), surface, reference) + layer = prism_layer((easting, northing), surface, reference) expected_mask = np.ones(shape, dtype=bool) for index in ((1, 2), (2, 3)): layer.top[index] = np.nan expected_mask[index] = False - mask = layer.prisms_layer._get_nonans_mask() + mask = layer.prism_layer._get_nonans_mask() npt.assert_allclose(mask, expected_mask) # Nans in bottom only # ------------------- - layer = prisms_layer((easting, northing), surface, reference) + layer = prism_layer((easting, northing), surface, reference) expected_mask = np.ones(shape, dtype=bool) for index in ((2, 1), (3, 2)): layer.bottom[index] = np.nan expected_mask[index] = False - mask = layer.prisms_layer._get_nonans_mask() + mask = layer.prism_layer._get_nonans_mask() npt.assert_allclose(mask, expected_mask) # Nans in top and bottom # ---------------------- - layer = prisms_layer((easting, northing), surface, reference) + layer = prism_layer((easting, northing), surface, reference) expected_mask = np.ones(shape, dtype=bool) for index in ((1, 2), (2, 3)): layer.top[index] = np.nan @@ -219,7 +219,7 @@ def test_nonans_prisms_mask(): for index in ((1, 2), (2, 1), (3, 2)): layer.bottom[index] = np.nan expected_mask[index] = False - mask = layer.prisms_layer._get_nonans_mask() + mask = layer.prism_layer._get_nonans_mask() npt.assert_allclose(mask, expected_mask) @@ -243,12 +243,12 @@ def test_nonans_prisms_mask_property(): surface[index] = np.nan density[index] = np.nan expected_mask[index] = False - layer = prisms_layer( + layer = prism_layer( (easting, northing), surface, reference, properties={"density": density} ) # Check if no warning is raised with warnings.catch_warnings(record=True) as warn: - mask = layer.prisms_layer._get_nonans_mask(property_name="density") + mask = layer.prism_layer._get_nonans_mask(property_name="density") assert len(warn) == 0 npt.assert_allclose(mask, expected_mask) @@ -267,19 +267,19 @@ def test_nonans_prisms_mask_property(): for index in indices: density[index] = np.nan expected_mask[index] = False - layer = prisms_layer( + layer = prism_layer( (easting, northing), surface, reference, properties={"density": density} ) # Check if warning is raised with warnings.catch_warnings(record=True) as warn: - mask = layer.prisms_layer._get_nonans_mask(property_name="density") + mask = layer.prism_layer._get_nonans_mask(property_name="density") assert len(warn) == 1 assert issubclass(warn[-1].category, UserWarning) npt.assert_allclose(mask, expected_mask) @pytest.mark.use_numba -def test_prisms_layer_gravity(): +def test_prism_layer_gravity(): """ Check if gravity method works as expected """ @@ -290,23 +290,23 @@ def test_prisms_layer_gravity(): reference = 0 surface = np.arange(20, dtype=float).reshape(shape) density = np.ones_like(surface, dtype=float) - layer = prisms_layer( + layer = prism_layer( (easting, northing), surface, reference, properties={"density": density} ) for field in ("potential", "g_z"): expected_result = prism_gravity( coordinates, - prisms=layer.prisms_layer._to_prisms(), + prisms=layer.prism_layer._to_prisms(), density=density, field=field, ) npt.assert_allclose( - expected_result, layer.prisms_layer.gravity(coordinates, field=field) + expected_result, layer.prism_layer.gravity(coordinates, field=field) ) @pytest.mark.use_numba -def test_prisms_layer_gravity_with_nans(): +def test_prism_layer_gravity_with_nans(): """ Check if gravity method works as expected when one of the prisms has nans """ @@ -321,7 +321,7 @@ def test_prisms_layer_gravity_with_nans(): for index in indices: surface[index] = np.nan density = np.ones_like(surface, dtype=float) - layer_nans = prisms_layer( + layer_nans = prism_layer( (easting, northing), surface, reference, properties={"density": density} ) # Create one layer that has zero density but no nans @@ -329,18 +329,18 @@ def test_prisms_layer_gravity_with_nans(): density = np.ones_like(surface, dtype=float) for index in indices: density[index] = 0 - layer_nonans = prisms_layer( + layer_nonans = prism_layer( (easting, northing), surface, reference, properties={"density": density} ) # Check if the two layers generate the same gravity field for field in ("potential", "g_z"): npt.assert_allclose( - layer_nans.prisms_layer.gravity(coordinates, field=field), - layer_nonans.prisms_layer.gravity(coordinates, field=field), + layer_nans.prism_layer.gravity(coordinates, field=field), + layer_nonans.prism_layer.gravity(coordinates, field=field), ) -def test_prisms_layer_gravity_density_nans(): +def test_prism_layer_gravity_density_nans(): """ Check if prisms is ignored after a nan is found in density array """ @@ -355,7 +355,7 @@ def test_prisms_layer_gravity_density_nans(): for index in indices: surface[index] = np.nan density = np.ones_like(surface, dtype=float) - layer_nans = prisms_layer( + layer_nans = prism_layer( (easting, northing), surface, reference, properties={"density": density} ) # Create one layer that has zero density but no nans @@ -363,12 +363,12 @@ def test_prisms_layer_gravity_density_nans(): density = np.ones_like(surface, dtype=float) for index in indices: density[index] = 0 - layer_nonans = prisms_layer( + layer_nonans = prism_layer( (easting, northing), surface, reference, properties={"density": density} ) # Check if the two layers generate the same gravity field for field in ("potential", "g_z"): npt.assert_allclose( - layer_nans.prisms_layer.gravity(coordinates, field=field), - layer_nonans.prisms_layer.gravity(coordinates, field=field), + layer_nans.prism_layer.gravity(coordinates, field=field), + layer_nonans.prism_layer.gravity(coordinates, field=field), )