Skip to content

Commit

Permalink
fix: add zref and density args to inversion (#24)
Browse files Browse the repository at this point in the history
* fix: add zref and density args to inversion

* fix: removes references to density and zref

* fix: remove python 3.12 support, add test for 3.10

* chore: specify semantic release options

* chore: remove pypy from testing versions
  • Loading branch information
mdtanker committed Nov 24, 2023
1 parent 1f99c55 commit c62f189
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 41 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,9 @@ jobs:
# run all of them to catch failures in different combinations.
fail-fast: false
matrix:
python-version: ["3.8", "3.11", "3.12"]
python-version: ["3.8", "3.10", "3.11"]
runs-on: [ubuntu-latest, macos-latest, windows-latest]

include:
- python-version: pypy-3.9
runs-on: ubuntu-latest
env:
FORCE_COLOR: 3
NUMBA_DISABLE_JIT: "1"
Expand Down
19 changes: 18 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering",
"Typing :: Typed",
]
Expand Down Expand Up @@ -214,3 +213,21 @@ build_command = """
python -m pip install build
python -m build .
"""
commit_parser = "angular"

[tool.semantic_release.commit_parser_options]
allowed_tags = [
"build", # Changes that affect the build system or external dependencies
"chore", # Changes to the build process or auxiliary tools and libraries such as documentation generation
"ci", # CI related changes
"docs", # Documentation only changes
"feat", # A new feature
"fix", # A bug fix
"perf", # A code change that improves performance
"style", # Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
"refactor", # A code change that neither fixes a bug nor adds a feature
"test", # Adding missing or correcting existing tests
]
# including BREAKING CHANGE: in the commit message will trigger a major release
minor_tags = ["feat"] # tags which result in minor releases
patch_tags = ["fix", "perf"] # tags which result in patch releases
12 changes: 8 additions & 4 deletions src/invert4geom/inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,8 @@ def run_inversion(
input_grav: pd.DataFrame,
input_grav_column: str,
prism_layer: xr.Dataset,
density_contrast: float,
zref: float,
max_iterations: int,
l2_norm_tolerance: float = 0.2,
delta_l2_norm_tolerance: float = 1.001,
Expand Down Expand Up @@ -765,6 +767,12 @@ def run_inversion(
column name containing the gravity data *before* regional separation
prism_layer : xr.Dataset
starting prism layer
density_contrast : float
density contrast of the prisms layer, should be same value used to create the
starting model
zref : float
reference height of the prisms layer, should be same value used to create the
starting model
max_iterations : int
the maximum allowed iterations, inclusive and starting at 1
l2_norm_tolerance : float, optional
Expand Down Expand Up @@ -812,15 +820,11 @@ def run_inversion(
(
prisms_df,
prisms_ds,
density_contrast,
zref,
prism_spacing,
_,
) = utils.extract_prism_data(prism_layer)

logging.info("extracted zref is %s", zref)
logging.info("extracted prism spacing is %s", prism_spacing)
logging.info("extracted density contrast is %s", density_contrast)

# create empty jacobian matrix
empty_jac: NDArray = np.empty(
Expand Down
34 changes: 5 additions & 29 deletions src/invert4geom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,17 +438,13 @@ def extract_prism_data(
pd.DataFrame,
xr.Dataset,
float,
float,
float,
xr.DataArray,
]:
"""
extract necessary info from starting prism layer, adds variables 'topo' and
'starting_topo' to prism layer dataset (prisms_ds), converts it into dataframe
(prisms_df), gets the density contrast value (density) from the max density value in
prisms_df, gets the reference level (zref) from the min value of the prims tops,
gets the prism spacing (spacing) from prisms_ds, and creates a grid of the starting
topography (topo_grid) from the tops and bottoms of the prism layer.
(prisms_df), gets the prism spacing (spacing) from prisms_ds, and creates a grid of
the starting topography (topo_grid) from the tops and bottoms of the prism layer.
Parameters
----------
Expand All @@ -457,32 +453,12 @@ def extract_prism_data(
Returns
-------
tuple
prisms_df, prisms_ds, density_contrast, zref, spacing, topo_grid)
tuple[pd.DataFrame, pd.Dataset, float, xr.DataArray]
prisms_df, prisms_ds, spacing, topo_grid
"""

prisms_ds = copy.deepcopy(prism_layer.load())

# check that minimum elevation of prism tops is equal to max elevation of prism
# bottoms
# if not prisms_ds.top.to_numpy().min() == prisms_ds.bottom.to_numpy().max():
# msg = "reference for prism layer is outside limits of tops and bottoms"
# raise ValueError(msg)

# check prisms above reference have densities of opposite sign to prisms below
# try:
# if not prisms_ds.density.to_numpy().max() == -prisms_ds.density.to_numpy().min(): # noqa: E501
# msg = "densities should be represented as contrasts not absolutes."
# raise ValueError(msg)
# # if not, they should at least be equal (if starting topo model is flat)
# except:
# if not prisms_ds.density.to_numpy().max() == prisms_ds.density.to_numpy().min(): # noqa: E501
# msg = "densities should be represented as contrasts not absolutes."
# raise ValueError(msg)

density_contrast = prisms_ds.density.to_numpy().max()
zref = prisms_ds.top.to_numpy().min()

# add starting topo to dataset
topo_grid = xr.where(prisms_ds.density > 0, prisms_ds.top, prisms_ds.bottom)
prisms_ds["topo"] = topo_grid
Expand All @@ -493,7 +469,7 @@ def extract_prism_data(

spacing = get_spacing(prisms_df)

return prisms_df, prisms_ds, density_contrast, zref, spacing, topo_grid
return prisms_df, prisms_ds, spacing, topo_grid


def get_spacing(prisms_df: pd.DataFrame) -> float:
Expand Down
4 changes: 1 addition & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,7 @@ def test_extract_prism_data():
"""
prism_layer = dummy_prism_layer()
results = utils.extract_prism_data(prism_layer)
prisms_df, prisms_ds, density_contrast, zref, spacing, topo_grid = results
assert density_contrast == 2670
assert zref == 0
prisms_df, prisms_ds, spacing, topo_grid = results
assert spacing == 200
expected = np.array([[0.0, 0.0, 0.0], [-30.0, -30.0, -30.0], [30.0, 30.0, 30.0]])
npt.assert_array_equal(expected, topo_grid)
Expand Down

0 comments on commit c62f189

Please sign in to comment.