Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progressbar to prism forward gravity calculations #315

Merged
merged 15 commits into from
Jun 16, 2022

Conversation

mdtanker
Copy link
Member

Description

This PR adds an optional progress bar to the forward gravity calculation of prisms_gravity. It uses numba_progress, and is called with a flag progressbar=True, which defaults to False. Currently it only works properly with parallel=True.

Relevant issues/PRs

Fixes #312

@welcome
Copy link

welcome bot commented May 24, 2022

💖 Thank you for opening your first pull request in this repository! 💖

A few things to keep in mind:

No matter what, we are really grateful that you put in the effort to do this!

Copy link
Member

@santisoler santisoler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking awesome @mdtanker !

I left some small suggestions to improve the code. Nothing critical, just a few ideas.

Besides those, the only thing we need is to write some tests for this. Feel free to ask for help if you need some!

harmonica/forward/prism.py Outdated Show resolved Hide resolved
harmonica/forward/prism.py Outdated Show resolved Hide resolved
harmonica/forward/prism.py Outdated Show resolved Hide resolved
harmonica/forward/prism_layer.py Outdated Show resolved Hide resolved
harmonica/forward/prism.py Outdated Show resolved Hide resolved
harmonica/forward/prism.py Outdated Show resolved Hide resolved
mdtanker and others added 4 commits May 30, 2022 08:31
Co-authored-by: Santiago Soler <santiago.r.soler@gmail.com>
removed progressbar=False from prism_layer.py

Co-authored-by: Santiago Soler <santiago.r.soler@gmail.com>
@mdtanker
Copy link
Member Author

Thanks for all the suggestions @santisoler. I've added them all in and have pushed the new commits. Any guidance for the tests would be appreciated!

@santisoler
Copy link
Member

Thanks for applying those changes @mdtanker ! This is looking great.

Regarding the tests. Harmonica test functions live in harmonica/tests. Each test function should be named test_... so pytest can know that it should run it.

There are different types of tests that we can run. The most important ones in Harmonica are the ones that check if a computation is producing accurate results. These are critical for ensuring that our tools compute what they meant. In harmonica/tests/test_prism.py we have several test functions for the prism_gravity function. I would add a function in there that checks if the results when passing progressbar=True are the same as the ones obtained when passing progressbar=False.

The same type of function should be added to harmonica/tests/prism_layer.py but for the prism layer.

You'll see that the test functions usually have a bunch of decorators (those lines that start with @). They are intended to use some of the nice tools provided by pytest like parametrization (running the same test under different scenarios) or fixtures (create sample objects that test functions use as input, like sample computation points or a sample collection of prisms). You can learn more about them in pytest docs: https://docs.pytest.org/en/6.2.x/parametrize.html

Besides, you'll find that we often use two different decorators:

  • @pytest.mark.use_numba
  • @run_only_with_numba

The first decorator should be added to every test function that tests a code written in Numba, while the second one is intended to be used on those test functions that take too long to run if Numba JIT is disabled.
Don't worry too much about the second one for now, but on every test function that will be computing the gravity of prisms use the first one.

Besides those there are other type of tests we should run, which are more intended to guarantee that the user interface works as expected. For instance, we need to check that the prism_gravity function raises a ImportError if numba_progress is not installed. Check out how we do it for pyvista:

@patch("harmonica.visualization.prism.pyvista", None)
def test_prisms_pyvista_missing_error(prisms, density):
"""
Check if prism_to_pyvista raises error if pyvista is missing
"""
# Check if error is raised
with pytest.raises(ImportError):
prism_to_pyvista(prisms, properties={"density": density})

When you run make test you'll get a detailed report of which tests passes, which fails and also which lines are being tested and which aren't. The percentage of code tested is called test coverage. We aim to hit 100% test coverage, although sometimes it isn't possible. But always remember that 100% test coverage doesn't mean good tests. So we prioritize test quality over test coverage.

Feel free to ask for help. We can also add code to this branch if you need, so don't feel obliged to write every possible test.

@santisoler
Copy link
Member

santisoler commented May 31, 2022

Yesterday I was running some benchmarks on this branch against the code we have in main to test if there was any performance hit on adding the progress bar.

Here I leave the script I used for benchmarking:

import time
import numpy as np
import verde as vd
import harmonica as hm

region = (0, 100e3, -40e3, 40e3)
spacing = 1000

easting, northing = vd.grid_coordinates(region=region, spacing=spacing)

wavelength = 24 * spacing
surface = np.abs(np.sin(easting * 2 * np.pi / wavelength))

density = np.full_like(surface, 2700)

prisms = hm.prism_layer(
    coordinates=(easting, northing),
    surface=surface,
    reference=0,
    properties={"density": density},
)

region_pad = vd.pad_region(region, 10e3)
coordinates = vd.grid_coordinates(region_pad, spacing=spacing, extra_coords=1e3)

# Run forward modelling once so Numba can compile
print("\nRunning first time for compilation")
prisms.prism_layer.gravity(coordinates, field="g_z")

# Then benchmark it
print("\nStarting benchmark")

times = []
for i in range(5):
    print(f"Running iteration {i}")
    start = time.time()
    gravity = prisms.prism_layer.gravity(coordinates, field="g_z")
    end = time.time()
    times.append(end - start)

print(f"Results: {np.mean(times)} +/- {np.std(times)}")

I run it as it is with the code in main to have a reference, and with the code in this branch choosing progressbar=True and progressbar=False. The results I got were:

Type of benchmark Time elapsed (s)
main branch 8.3 ± 0.4
progressbar=True 8.2 ± 0.8
progressbar=False 8.4 ± 0.4

These specific values depend on the hardware setting and your cpu load at the time of the benchmark

In conclusion, I don't see any appreciable performance hit, not even comparing this branch with main, and not even choosing progressbar=True or progressbar=False.

@mdtanker
Copy link
Member Author

mdtanker commented Jun 9, 2022

@santisoler thanks for the help with the tests. I've added test which I believe accomplish what we want. make test results are below.

----------- coverage: platform linux, python 3.9.0-final-0 -----------
Name                                                                              Stmts   Miss Branch BrPart  Cover   Missing
-----------------------------------------------------------------------------------------------------------------------------
/Users/home/tankerma/harmonica/harmonica/_version.py                                  3      0      0      0   100%
/Users/home/tankerma/harmonica/harmonica/constants.py                                 2      0      0      0   100%
/Users/home/tankerma/harmonica/harmonica/datasets/sample_data.py                     52      0      0      0   100%
/Users/home/tankerma/harmonica/harmonica/equivalent_sources/cartesian.py             88      0     24      0   100%
/Users/home/tankerma/harmonica/harmonica/equivalent_sources/gradient_boosted.py      79      0     30      0   100%
/Users/home/tankerma/harmonica/harmonica/equivalent_sources/spherical.py             66      0     12      0   100%
/Users/home/tankerma/harmonica/harmonica/equivalent_sources/utils.py                 29      0     14      0   100%
/Users/home/tankerma/harmonica/harmonica/forward/_tesseroid_utils.py                162      0     66      0   100%
/Users/home/tankerma/harmonica/harmonica/forward/_tesseroid_variable_density.py      67      0     16      0   100%
/Users/home/tankerma/harmonica/harmonica/forward/point.py                           103      0     22      0   100%
/Users/home/tankerma/harmonica/harmonica/forward/prism.py                            98      2     50      0    99%   141-142
/Users/home/tankerma/harmonica/harmonica/forward/prism_layer.py                     108      0     26      0   100%
/Users/home/tankerma/harmonica/harmonica/forward/tesseroid.py                        68      0     26      0   100%
/Users/home/tankerma/harmonica/harmonica/forward/utils.py                            57      0      8      0   100%
/Users/home/tankerma/harmonica/harmonica/gravity_corrections.py                      11      0      0      0   100%
/Users/home/tankerma/harmonica/harmonica/io.py                                       70      0     42      0   100%
/Users/home/tankerma/harmonica/harmonica/isostasy.py                                 18      0      2      0   100%
/Users/home/tankerma/harmonica/harmonica/synthetic/surveys.py                        34      0      6      0   100%
/Users/home/tankerma/harmonica/harmonica/visualization/prism.py                      31      2     12      0    95%   14-15
-----------------------------------------------------------------------------------------------------------------------------
TOTAL                                                                              1146      4    356      0    99%

================================================================================== short test summary info ===================================================================================
FAILED ../harmonica/forward/tesseroid.py::harmonica.forward.tesseroid.tesseroid_gravity
================================================================== 1 failed, 281 passed, 121 skipped, 56 warnings in 26.22s ==================================================================
make: *** [Makefile:26: test_coverage] Error 1

It resulted in 1 failure, which was on harmonica.forward.tesseroid.tesseroid_gravity so I don't think that is related. The tests had 99% coverage for prism.py. It was missing lines 141-142, the except statement in the following:

# Attempt to import numba_progress
try:
    from numba_progress import ProgressBar
except ImportError:
    ProgressBar = None

Here was my test for whether numba_progress was installed, not sure if I did this properly.

@patch("numba_progress.ProgressBar", None)
def test_numba_progress_missing_error():
    """
    Check if error is raised when progresbar=True and numba_progress package 
    is not installed.
    """
    prisms = [
        [-100, 0, -100, 0, -10, 0],
        [0, 100, -100, 0, -10, 0],
        [-100, 0, 0, 100, -10, 0],
        [0, 100, 0, 100, -10, 0],
    ]
    densities = [2000, 3000, 4000, 5000]
    coordinates = [0, 0, 0]
    # Check if error is raised
    with pytest.raises(ImportError):
        prism_gravity(
            coordinates, prisms, densities, field="potential", progressbar=True
        )

Copy link
Member

@santisoler santisoler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great @mdtanker!

The tesseroid test error is probably unrelated. If that's the case we will handle it on a separate PR, so don't worry.

It's ok not to achieve 100% of coverage in this case. The try: import... lines are hard to test: we would need to mimic a missing numba_progress at import time. It's a tricky problem and we don't get much out of it besides a not very meaningful 100%. So it's ok if we leave those lines untested. The test for the custom ImportError that prism_gravity raises is great though! I only have a small suggestion on it, check comment below.

As you can see now we are running CIs on this branch to check that everything passes. We would need to add numba_progress as a requirement for running the tests. Please edit the env/requirements-test.txt file and add it there. CIs are also complaining about code style, that one is easy: just run make format to autoformat the code to comply with PEP8.

Thanks again @mdtanker ! Let me know if you need any help!

harmonica/forward/prism.py Outdated Show resolved Hide resolved
harmonica/tests/test_prism.py Show resolved Hide resolved
harmonica/tests/test_prism_layer.py Show resolved Hide resolved
@santisoler
Copy link
Member

@mdtanker This is looking great. I think this is ready to be merged. Is there something else you want to add? We can always change things later, no problem.

@mdtanker
Copy link
Member Author

@mdtanker This is looking great. I think this is ready to be merged. Is there something else you want to add? We can always change things later, no problem.

Merge away! Thanks for all the help.

@santisoler santisoler changed the title added progress bar for forward gravity calculations Add progressbar to prism forward gravity calculations Jun 16, 2022
@santisoler santisoler merged commit cc697af into fatiando:main Jun 16, 2022
@welcome
Copy link

welcome bot commented Jun 16, 2022

🎉 Congrats on merging your first pull request and welcome to the team! 🎉

If you would like to be added as a author on the Zenodo archive of the next release, add your full name, affiliation, and ORCID (optional) to the AUTHORS.md file of this repository. Feel free to do this in a new pull request if needed.

We hope that this was a good experience for you. Let us know if there is any way that the contributing process could be improved.

@mdtanker mdtanker deleted the numba_progress_branch branch June 16, 2022 21:07
LL-Geo added a commit to LL-Geo/harmonica that referenced this pull request Jun 23, 2022
commit 54c3cbe52bd33a94ccbb5bb44f2958bb3afc9330
Author: LL-Geo <54405391+LL-Geo@users.noreply.github.com>
Date:   Thu Jun 23 21:36:23 2022 +0800

    Update filter

    Add more filter

commit 4a5d6f1
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Thu Jun 16 18:00:32 2022 -0300

    Avoid checking floats in tesseroid doctests (fatiando#326)

    Remove expected results for tesseroid calculations in docstring examples.
    Printing floats in forward modelling examples isn't that meaningful and often
    creates failures when running doctests: small differences between the expected
    and the got value could occur under some dependency and OS combinations.

commit cc697af
Author: Matt Tankersley <81199856+mdtanker@users.noreply.github.com>
Date:   Fri Jun 17 08:24:32 2022 +1200

    Add progressbar to prism forward gravity calculations (fatiando#315)

    Add optional `progressbar` flag to `prism_gravity` function and to the
    `gravity` method of the prism layer accesor to print a progress bar using
    `numba_progress`. Add `numba_progress` as optional dependency. Add test
    functions for the new feature.

commit 5a1c895
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Tue Jun 14 13:20:39 2022 -0300

    Specify spherical latitude in point sources guide (fatiando#325)

    Replaces latitude for spherical latitude in another place of the
    `point.rst`. Fix typo on "Alternatively".

commit cb476b2
Author: Federico Esteban <federico.esteban@gmail.com>
Date:   Tue Jun 14 11:33:22 2022 -0300

    Note that spherical and geodetic latitudes are equal in spherical ellipsoids (fatiando#324)

    Add sentence in the Coordinate Systems section of the User Guide noting that
    if the reference ellipsoid were a sphere both the spherical latitude and the
    geodetic latitude are equivalent.

commit 1256ff6
Author: Federico Esteban <federico.esteban@gmail.com>
Date:   Mon Jun 13 11:35:02 2022 -0300

    Add Federico Esteban to AUTHORS.md (fatiando#323)

    Add his name, link to his GitHub account, affiliation and ORCID number.

commit 32de6e0
Author: Federico Esteban <federico.esteban@gmail.com>
Date:   Thu Jun 9 15:44:48 2022 -0300

    Specify "spherical latitude" when describing coordinates of point masses (fatiando#321)

    Add "spherical" when describing the spherical latitude coordinate of point
    masses in the user guide. This way we differentiate it from the "latitude"
    geodetic coordinate.

commit 9667fab
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Mon Jun 6 11:05:17 2022 -0300

    Fix small format errors in the user guide (fatiando#319)

    Fix link to EquivalentSources.predict method and fix superscripts in the docs.

commit 2f7fcb6
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Fri Jun 3 11:17:51 2022 -0300

    Update docs and create a proper user guide (fatiando#305)

    Update Sphinx docs using sphinx-panels.
    Add a proper User Guide that will ultimately replace the gallery examples.
    Each page of the new User Guide is a .rst file that uses jupyter-sphinx to run example code blocks.
    Added pages for: Coordinate systems, Forward Modelling, Gravity corrections and Equivalent Sources.
    Added a new doc/versions.rst file with links to previous documentations.

commit cf4080c
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Tue May 31 15:58:25 2022 -0300

    Compute upward derivative of a grid in the frequency domain (fatiando#238)

    Define a new derivative_upward function for computing the spatial upward
    derivative of a 2D grid in the frequency domain. The function makes use of
    xrft for handling Fourier transformations of xarray objects. Add a new
    filters subpackage that includes FFT filters: functions that take grids in
    frequency domain and return the desired filter also in frequency domain. Add
    fft and ifft wrapper functions of the xrft.fft and xrft.ifft ones. Add
    a new apply_filter function that takes a grid in the spatial domain, applies
    fft, the filter and ifft and returns the filtered grid also in spatial domain.
    Add tests for the new features and a gallery example for the upward derivative.
    Add netcdf4 as requirement for testing.

commit 6a30797
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Fri May 27 16:22:18 2022 -0300

    Ditch soon-to-be deprecated args of equivalent sources grid method (fatiando#311)

    The grid() method of Verde gridders now take a coordinates argument with
    the coordinates of the target grid. The previous region, shape and
    spacing arguments will be deprecated in Verde v2.0.0. This change makes it
    easier for our equivalent sources classes: we don't need the extra upward
    argument, users can create the coordinates of the target grid using
    verde.grid_coordinates and pass them via coordinates argument. Ditch the
    upward, shape, spacing and region arguments from the equivalent sources
    gridders. Replace them for the new coordinates argument: users need to
    provide the coordinates of the target grid instead of building it through the
    grid method. Raise errors if any of those old arguments are being passed. Raise
    warnings if any kwargs are passed: they are being ignored and not passed to the
    BaseGridder.grid() method.

commit 51ceb7e
Author: Agustina <pesce.agustina@gmail.com>
Date:   Mon May 23 11:03:54 2022 -0300

    Remove deprecated point_mass_gravity function (fatiando#310)

    Remove point_mass_gravity function from harmonica because it was deprecated on
    PR fatiando#280. Remove related test functions.

commit f336aa8
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Thu May 5 14:52:08 2022 -0300

    Drop support for Python 3.6 (fatiando#309)

    Remove the compatibility metadata, remove from the CI matrix, bump the
    python_requires to 3.7+.

commit d132abb
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Tue May 3 12:47:22 2022 -0300

    Add computation of gravitational tensor components for point sources (fatiando#288)

    Add new kernel functions to compute gravity tensor components generated by
    point sources. Add test functions for the new feature: check that the diagonal
    elements satisfy the Laplace equation, compare all components against finite
    difference computations from the gravity acceleration. Add test class for
    checking the symmetry of tensor components. Refactor old test functions for
    point gravity: merge some functions into single ones through pytest
    parametrizations. Avoid using "gradient" for specifying the gravity
    acceleration vector: the "gravity gradient" is usually used to refer to the
    tensor.

commit eb71d54
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Fri Apr 22 17:23:00 2022 -0300

    Add deprecations to datasets and synthetic modules (fatiando#304)

    Add FutureWarnings to public functions of the synthetic and dataset
    modules. Add tests for the new warnings. Both modules will be deprecated in
    Harmonica v0.6.0. Instead of providing sample datasets, Harmonica will depend
    on Ensaio for that. The synthetic surveys depend on some of the sample
    datasets, but those functions are intended to be used in methodology articles,
    so they should live somewhere else.

commit a4598ef
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Fri Apr 22 17:06:43 2022 -0300

    Add conversion of prisms or a prism layer to PyVista objects (fatiando#291)

    Add a new visualization module that hosts prism_to_pyvista: a function to
    convert a set of prisms into a pyvista.UnstructuredGrid. Include the new
    module and this function in the API Reference. Add a new to_pyvista() method
    to the PrismLayer accessor that converts a prism layer into a pyvista grid,
    making it easier to plot it in 3D. The UnstructuredGrid has the information
    about each prism as hexahedrons, along with their physical properties as cell
    data. Add tests for the new features. Add pyvista and vtk as optional
    dependencies to environment.yml and setup.cfg. Add a new example for
    plotting a PrismLayer. Configure Sphinx to show pyvista plots in the gallery
    and to use the pyvista-plot directive in docstrings.

commit 762d210
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Mon Apr 4 14:09:45 2022 -0300

    Update Black to its stable version (fatiando#301)

    Black has released a stable version: 22.3.0. Now the style check tests use this
    version. Fixes a bug on CI in which Black was trying to import a private module
    of click that doesn't exist anymore. Rerun black: now Black hugs simple
    power operators.

commit 10577fa
Author: Santiago Soler <santiago.r.soler@gmail.com>
Date:   Mon Apr 4 14:00:42 2022 -0300

    Update Sphinx version to 4.5.0 (fatiando#302)

    Updates also sphinx gallery and sphinx book theme.
    This fixes a issue between latest jinja2 and Sphinx 3.5.*.

commit f880065
Author: Leonardo Uieda <leouieda@gmail.com>
Date:   Fri Mar 18 13:34:51 2022 +0000

    Move configuration from setup.py to setup.cfg (fatiando#296)

    Make the move away from setup.py following the recommendations from the
    Python packaging guides. Moves the requirement listing to setup.cfg as
    well and will use a script to extract this for conda installing on CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding progress bar to forward gravity calculation of prisms
2 participants