Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ information.
You'll find more information about project maintenance (releases, reviews, etc)
in our organization-wide [Maintainers Guide][maintenance].

## About coverage analysis

Harmonica relies on numba for performance within some functions, which can make
it a bit difficult to measure test coverage since the functions being
executed are the jit-compiled versions of the functions in the code.
Fortunately, numba now supports measuring coverage if the environment variable
`NUMBA_JIT_COVERAGE=1` is set. However, it still does not support branch
analysis and will report any `for` or `if` as missing coverage for a branch,
even if tests explicitly for that are added. To get around this, we have to
disable branch analysis for every single `for` and `if` statement in our code by
putting `# pragma: no branch` after the statement.

[coc]: https://github.com/fatiando/community/blob/main/CODE_OF_CONDUCT.md
[contrib]: https://github.com/fatiando/community/blob/main/CONTRIBUTING.md
[maintenance]: https://github.com/fatiando/community/blob/main/MAINTENANCE.md
Expand Down
13 changes: 3 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Build, package, test, and clean
STYLE_CHECK_FILES=src test doc

.PHONY: build install test test_coverage test_numba format check check-format check-style check-actions clean
.PHONY: build help install test format check check-format check-style check-actions clean

help:
@echo "Commands:"
Expand All @@ -14,21 +14,14 @@ help:
@echo " clean clean up build and generated files"
@echo ""

.PHONY: build, install, test, test_coverage, test_numba, format, check, check-format, check-style, check-actions, clean

build:
python -m build .

install:
python -m pip install --no-deps -e .

test: test_coverage test_numba

test_coverage:
NUMBA_DISABLE_JIT=1 MPLBACKEND='agg' pytest --cov

test_numba:
NUMBA_DISABLE_JIT=0 MPLBACKEND='agg' pytest -m use_numba
test:
NUMBA_JIT_COVERAGE=1 pytest

format:
ruff check --select I --fix $(STYLE_CHECK_FILES) # fix isort errors
Expand Down
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ convention = "numpy"

[tool.pytest.ini_options]
doctest_optionflags = "NUMBER"
markers = [
"use_numba: mark test functions that call Numba jitted functions"
]
addopts = "--verbose --doctest-modules --doctest-continue-on-failure"
addopts = "--verbose --cov --doctest-modules --doctest-continue-on-failure"
testpaths = [
"test",
"src",
Expand Down
8 changes: 4 additions & 4 deletions src/harmonica/_equivalent_sources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def jacobian(coordinates, points, jac, greens_function):
"""
east, north, upward = coordinates[:]
point_east, point_north, point_upward = points[:]
for i in numba.prange(east.size):
for j in range(point_east.size):
for i in numba.prange(east.size): # pragma: no branch
for j in range(point_east.size): # pragma: no branch
jac[i, j] = greens_function(
east[i],
north[i],
Expand All @@ -85,8 +85,8 @@ def predict(coordinates, points, coeffs, result, greens_function):
"""
east, north, upward = coordinates[:]
point_east, point_north, point_upward = points[:]
for i in numba.prange(east.size):
for j in range(point_east.size):
for i in numba.prange(east.size): # pragma: no branch
for j in range(point_east.size): # pragma: no branch
result[i] += coeffs[j] * greens_function(
east[i],
north[i],
Expand Down
40 changes: 22 additions & 18 deletions src/harmonica/_forward/_tesseroid_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ def gauss_legendre_quadrature(
# optimization: reduce the number of times that the trigonometric functions
# are evaluated)
result = 0.0
for j, lat_node in enumerate(lat_nodes):
for j, lat_node in enumerate(lat_nodes): # pragma: no branch
# Get the latitude of the point mass
latitude_p = np.radians(0.5 * (n - s) * lat_node + 0.5 * (n + s))
cosphi_p = np.cos(latitude_p)
sinphi_p = np.sin(latitude_p)
for k, rad_node in enumerate(rad_nodes):
for k, rad_node in enumerate(rad_nodes): # pragma: no branch
# Get the radius of the point mass
radius_p = 0.5 * (top - bottom) * rad_node + 0.5 * (top + bottom)
# Get kappa constant for the point mass
kappa = radius_p**2 * cosphi_p
for i, lon_node in enumerate(lon_nodes):
for i, lon_node in enumerate(lon_nodes): # pragma: no branch
# Get the longitude of the point mass
longitude_p = np.radians(0.5 * (e - w) * lon_node + 0.5 * (e + w))
# Compute the mass of the point mass
Expand Down Expand Up @@ -198,7 +198,7 @@ def _adaptive_discretization(
stack[0] = tesseroid
stack_top = 0
n_splits = 0
while stack_top >= 0:
while stack_top >= 0: # pragma: no branch
# Pop the first tesseroid from the stack
tesseroid = stack[stack_top]
stack_top -= 1
Expand All @@ -208,25 +208,29 @@ def _adaptive_discretization(
distance = _distance_tesseroid_point(coordinates, tesseroid)
# Check inequality
n_lon, n_lat, n_rad = 1, 1, 1
if distance / l_lon < distance_size_ratio:
if distance / l_lon < distance_size_ratio: # pragma: no branch
n_lon = 2
if distance / l_lat < distance_size_ratio:
if distance / l_lat < distance_size_ratio: # pragma: no branch
n_lat = 2
if distance / l_rad < distance_size_ratio and radial_discretization:
if (
distance / l_rad < distance_size_ratio and radial_discretization
): # pragma: no branch
n_rad = 2
# Apply discretization
if n_lon * n_lat * n_rad > 1:
if n_lon * n_lat * n_rad > 1: # pragma: no branch
# Raise error if stack overflow
# Number of tesseroids in stack = stack_top + 1
if (stack_top + 1) + n_lon * n_lat * n_rad > stack.shape[0]:
if (stack_top + 1) + n_lon * n_lat * n_rad > stack.shape[
0
]: # pragma: no branch
msg = "Stack Overflow. Try to increase the stack size."
raise OverflowError(msg)
stack_top = _split_tesseroid(
tesseroid, n_lon, n_lat, n_rad, stack, stack_top
)
else:
else: # pragma: no branch
# Raise error if small_tesseroids overflow
if n_splits + 1 > small_tesseroids.shape[0]:
if n_splits + 1 > small_tesseroids.shape[0]: # pragma: no branch
raise OverflowError(
"Exceeded maximum discretizations."
+ " Please increase the MAX_DISCRETIZATIONS."
Expand All @@ -246,9 +250,9 @@ def _split_tesseroid(tesseroid, n_lon, n_lat, n_rad, stack, stack_top):
d_lon = (e - w) / n_lon
d_lat = (n - s) / n_lat
d_rad = (top - bottom) / n_rad
for i in range(n_lon):
for j in range(n_lat):
for k in range(n_rad):
for i in range(n_lon): # pragma: no branch
for j in range(n_lat): # pragma: no branch
for k in range(n_rad): # pragma: no branch
stack_top += 1
stack[stack_top, 0] = w + d_lon * i
stack[stack_top, 1] = w + d_lon * (i + 1)
Expand Down Expand Up @@ -455,8 +459,8 @@ def _check_points_outside_tesseroids(coordinates, tesseroids):
"""
longitude, latitude, radius = coordinates[:]
conflicting = []
for i in range(longitude.size):
for j in range(tesseroids.shape[0]):
for i in range(longitude.size): # pragma: no branch
for j in range(tesseroids.shape[0]): # pragma: no branch
# Longitudinal boundaries of the tesseroid must be compared with
# longitudinal coordinates of computation points when moved to
# [0, 360) and [-180, 180).
Expand All @@ -467,7 +471,7 @@ def _check_points_outside_tesseroids(coordinates, tesseroids):
(west < longitude_180 < east or west < longitude_360 < east)
and south < latitude[i] < north
and bottom < radius[i] < top
):
): # pragma: no branch
conflicting.append((i, j))
return conflicting

Expand Down Expand Up @@ -511,7 +515,7 @@ def _discard_null_tesseroids(tesseroids, density):
Parameters
----------
tesseroids : 2d-array
Array containing the boundaries of the tesserois in the following
Array containing the boundaries of the tesseroids in the following
order: ``west``, ``east``, ``south``, ``north``, ``bottom``, ``top``
defined in a geocentric spherical coordinate system.
density : 1d-array
Expand Down
6 changes: 3 additions & 3 deletions src/harmonica/_forward/_tesseroid_variable_density.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ def gauss_legendre_quadrature_variable_density(
# optimization: reduce the number of times that the trigonometric functions
# are evaluated)
result = 0.0
for j, lat_node in enumerate(lat_nodes):
for j, lat_node in enumerate(lat_nodes): # pragma: no branch
# Get the latitude of the point mass
latitude_p = np.radians(0.5 * (n - s) * lat_node + 0.5 * (n + s))
cosphi_p = np.cos(latitude_p)
sinphi_p = np.sin(latitude_p)
for k, rad_node in enumerate(rad_nodes):
for k, rad_node in enumerate(rad_nodes): # pragma: no branch
# Get the radius of the point mass
radius_p = 0.5 * (top - bottom) * rad_node + 0.5 * (top + bottom)
density_p = density(radius_p)
# Get kappa constant for the point mass
kappa = radius_p**2 * cosphi_p
for i, lon_node in enumerate(lon_nodes):
for i, lon_node in enumerate(lon_nodes): # pragma: no branch
# Get the longitude of the point mass
longitude_p = np.radians(0.5 * (e - w) * lon_node + 0.5 * (e + w))
# Compute the mass of the point mass
Expand Down
12 changes: 6 additions & 6 deletions src/harmonica/_forward/dipole.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ def _jit_dipole_magnetic_field_cartesian(
easting_p, northing_p, upward_p = dipoles
mag_e, mag_n, mag_u = magnetic_moments
# Iterate over computation points and dipoles
for i in numba.prange(easting.size):
for j in range(easting_p.size):
for i in numba.prange(easting.size): # pragma: no branch
for j in range(easting_p.size): # pragma: no branch
easting_comp, northing_comp, upward_comp = magnetic_field(
easting[i],
northing[i],
Expand All @@ -343,7 +343,7 @@ def _jit_dipole_magnetic_field_cartesian(
b_n[i] += northing_comp
b_u[i] += upward_comp
# Update progress bar if called
if update_progressbar:
if update_progressbar: # pragma: no branch
progress_proxy.update(1)


Expand Down Expand Up @@ -383,8 +383,8 @@ def _jit_dipole_magnetic_component_cartesian(
easting_p, northing_p, upward_p = dipoles
mag_e, mag_n, mag_u = magnetic_moments
# Iterate over computation points and dipoles
for i in numba.prange(easting.size):
for j in range(easting_p.size):
for i in numba.prange(easting.size): # pragma: no branch
for j in range(easting_p.size): # pragma: no branch
result[i] += forward_func(
easting[i],
northing[i],
Expand All @@ -397,7 +397,7 @@ def _jit_dipole_magnetic_component_cartesian(
mag_u[j],
)
# Update progress bar if called
if update_progressbar:
if update_progressbar: # pragma: no branch
progress_proxy.update(1)


Expand Down
8 changes: 4 additions & 4 deletions src/harmonica/_forward/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ def point_mass_cartesian(
field on the computation points. It could be one of the forward
modelling functions in :mod:`choclo.point`.
"""
for i in numba.prange(easting.size):
for j in range(easting_p.size):
for i in numba.prange(easting.size): # pragma: no branch
for j in range(easting_p.size): # pragma: no branch
out[i] += forward_func(
easting[i],
northing[i],
Expand Down Expand Up @@ -433,8 +433,8 @@ def point_mass_spherical(
cosphi_p = np.cos(latitude_p)
sinphi_p = np.sin(latitude_p)
# Compute gravitational field
for i in numba.prange(longitude.size):
for j in range(longitude_p.size):
for i in numba.prange(longitude.size): # pragma: no branch
for j in range(longitude_p.size): # pragma: no branch
out[i] += masses[j] * kernel(
longitude[i],
cosphi[i],
Expand Down
Loading
Loading