Repository navigation
Add linearity tests for forward models - #698
Conversation
santisoler
left a comment
There was a problem hiding this comment.
Hi @manduinca! Sorry for the delayed review. I was busy at a conference and catching up with stuff.
This looks really nice, thanks again for taking the effort and work on it! I left some minor suggestions below. Nothing major, just to make a few portions of the tests more Pythonic. Please, let me know what do you think, and feel free to apply and modify the suggestions as you like.
| sample_prisms[i : i + 1], | ||
| sample_densities[i : i + 1], | ||
| field, | ||
| ) | ||
| for i in range(sample_densities.size) |
There was a problem hiding this comment.
Minor suggestion here: we don't need to index the arrays, we can iterate over them with a zip function:
| sample_prisms[i : i + 1], | |
| sample_densities[i : i + 1], | |
| field, | |
| ) | |
| for i in range(sample_densities.size) | |
| prism, | |
| density, | |
| field, | |
| ) | |
| for prism, density in zip(sample_prisms, sample_densities, strict=True) |
| sample_tesseroids[i : i + 1], | ||
| sample_densities[i : i + 1], | ||
| field=field, | ||
| ) | ||
| for i in range(sample_densities.size) |
There was a problem hiding this comment.
Similarly here:
| sample_tesseroids[i : i + 1], | |
| sample_densities[i : i + 1], | |
| field=field, | |
| ) | |
| for i in range(sample_densities.size) | |
| tesseroid, | |
| density, | |
| field=field, | |
| ) | |
| for tesseroid, density in zip(sample_tesseroids, sample_densities, strict=True) |
| tuple(p[i : i + 1] for p in sample_points), | ||
| sample_masses[i : i + 1], |
There was a problem hiding this comment.
I would suggest indexing the arrays directly, so we pass only a float for each coordinate and for the mass. This way we are also testing (although we are probably already testing that in other places) that passing just a float as coordinates and single float for mass also works.
| tuple(p[i : i + 1] for p in sample_points), | |
| sample_masses[i : i + 1], | |
| tuple(p[i] for p in sample_points), | |
| sample_masses[i], |
| tuple(p[i : i + 1] for p in points), | ||
| sample_masses[i : i + 1], |
There was a problem hiding this comment.
Similarly here:
| tuple(p[i : i + 1] for p in points), | |
| sample_masses[i : i + 1], | |
| tuple(p[i] for p in points), | |
| sample_masses[i], |
| tuple(d[i : i + 1] for d in sample_dipoles), | ||
| tuple(m[i : i + 1] for m in sample_magnetic_moments), |
There was a problem hiding this comment.
And here:
| tuple(d[i : i + 1] for d in sample_dipoles), | |
| tuple(m[i : i + 1] for m in sample_magnetic_moments), | |
| tuple(d[i] for d in sample_dipoles), | |
| tuple(m[i] for m in sample_magnetic_moments), |
| sample_prisms[i : i + 1], | ||
| tuple(m[i : i + 1] for m in sample_magnetizations), |
There was a problem hiding this comment.
| sample_prisms[i : i + 1], | |
| tuple(m[i : i + 1] for m in sample_magnetizations), | |
| sample_prisms[i], | |
| tuple(m[i] for m in sample_magnetizations), |
|
Thanks for the review @santisoler! I applied all six suggestions: Ran |
Test that the field generated by a set of sources equals the sum of the fields of each source computed separately. Covers point_gravity (Cartesian and spherical), dipole_magnetic, prism_gravity, prism_magnetic and tesseroid_gravity, parametrized by field.
df010e5 to
0aa1d05
Compare
|
@santisoler this one has been approved since 18 August, and after the rebase on 14 September the CI is fully green (14/14) — the earlier red Is there anything left on my side, or is it just waiting for a merge window? |
|
@santisoler with the new AI policy in place, I want to be upfront about this one: the linearity tests were written with Claude Code (an AI coding agent) following my direction, and so were my replies in this thread, this one included. The policy came after I opened the PR. If it doesn't fit, I'm fine with you closing it. |
|
Hi @manduinca. Thanks for disclosing your usage of AI in this PR. Since we already put effort from our side on reviewing this PR, I'll move forward and merge it. The issue that this PR solves was labeled as a You are more than welcome to make future contributions to Harmonica or to any other of the Fatiando projects. Just please be mindful on our AI Usage Policy and our preference of human-created content. |
Fixes #293
Adds
test/test_linearity.pywith linearity tests for each geometry (points, prisms, tesseroids), parametrized by field, following the discussion in the issue: the field of a set of sources is compared against the sum of the fields of each source computed separately.Includes both gravity and magnetic forward models:
point_gravity(Cartesian and spherical coordinates),dipole_magnetic,prism_gravity,prism_magneticandtesseroid_gravity, using mixed-sign masses/densities/magnetizations.The newer
ellipsoid_gravity/ellipsoid_magneticforwards could get the same treatment in a follow-up if you'd like.Squash and merge summary
Add linearity tests for the forward modelling functions of points, dipoles, prisms and tesseroids: check if the fields obtained after passing a collection of sources equals to the sum of the fields of each source computed independently.