Add linearity tests for forward models - #698
Conversation
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.
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), |
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.